001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.validator.routines; 018 019import java.io.Serializable; 020import java.util.List; 021import java.util.regex.Matcher; 022import java.util.regex.Pattern; 023 024/** 025 * <b>Regular Expression</b> validation (using the JRE's regular expression support). 026 * <p> 027 * Constructs the validator either for a single regular expression or a set (array) of 028 * regular expressions. By default validation is <i>case sensitive</i> but constructors 029 * are provided to allow <i>case in-sensitive</i> validation. For example to create 030 * a validator which does <i>case in-sensitive</i> validation for a set of regular 031 * expressions: 032 * </p> 033 * <pre> 034 * <code> 035 * String[] regexs = new String[] {...}; 036 * RegexValidator validator = new RegexValidator(regexs, false); 037 * </code> 038 * </pre> 039 * 040 * <ul> 041 * <li>Validate {@code true} or {@code false}:</li> 042 * <li> 043 * <ul> 044 * <li><code>boolean valid = validator.isValid(value);</code></li> 045 * </ul> 046 * </li> 047 * <li>Validate returning an aggregated String of the matched groups:</li> 048 * <li> 049 * <ul> 050 * <li><code>String result = validator.validate(value);</code></li> 051 * </ul> 052 * </li> 053 * <li>Validate returning the matched groups:</li> 054 * <li> 055 * <ul> 056 * <li><code>String[] result = validator.match(value);</code></li> 057 * </ul> 058 * </li> 059 * </ul> 060 * 061 * <b>Note that patterns are matched against the entire input.</b> 062 * 063 * <p> 064 * Cached instances pre-compile and re-use {@link Pattern}(s) - which according 065 * to the {@link Pattern} API are safe to use in a multi-threaded environment. 066 * </p> 067 * 068 * @since 1.4 069 */ 070public class RegexValidator implements Serializable { 071 072 private static final long serialVersionUID = -8832409930574867162L; 073 074 private final Pattern[] patterns; 075 076 /** 077 * Constructs a <i>case sensitive</i> validator that matches any one 078 * in the list of regular expressions. 079 * 080 * @param regexs The set of regular expressions this validator will 081 * validate against 082 */ 083 RegexValidator(final List<String> regexs) { 084 this(regexs.toArray(new String[] {}), true); 085 } 086 087 /** 088 * Constructs a <i>case sensitive</i> validator for a single 089 * regular expression. 090 * 091 * @param regex The regular expression this validator will 092 * validate against 093 */ 094 public RegexValidator(final String regex) { 095 this(regex, true); 096 } 097 098 /** 099 * Constructs a <i>case sensitive</i> validator that matches any one 100 * in the array of regular expressions. 101 * 102 * @param regexs The set of regular expressions this validator will 103 * validate against 104 */ 105 public RegexValidator(final String... regexs) { 106 this(regexs, true); 107 } 108 109 /** 110 * Constructs a validator for a single regular expression 111 * with the specified case sensitivity. 112 * 113 * @param regex The regular expression this validator will 114 * validate against 115 * @param caseSensitive when {@code true} matching is <i>case 116 * sensitive</i>, otherwise matching is <i>case in-sensitive</i> 117 */ 118 public RegexValidator(final String regex, final boolean caseSensitive) { 119 this(new String[] { regex }, caseSensitive); 120 } 121 122 /** 123 * Constructs a validator that matches any one of the set of regular 124 * expressions with the specified case sensitivity. 125 * 126 * @param regexs The set of regular expressions this validator will 127 * validate against 128 * @param caseSensitive when {@code true} matching is <i>case 129 * sensitive</i>, otherwise matching is <i>case in-sensitive</i> 130 */ 131 public RegexValidator(final String[] regexs, final boolean caseSensitive) { 132 if (regexs == null || regexs.length == 0) { 133 throw new IllegalArgumentException("Regular expressions are missing"); 134 } 135 patterns = new Pattern[regexs.length]; 136 final int flags = caseSensitive ? 0 : Pattern.CASE_INSENSITIVE; 137 for (int i = 0; i < regexs.length; i++) { 138 final String regex = regexs[i]; 139 if (regex == null || regex.isEmpty()) { 140 throw new IllegalArgumentException("Regular expression[" + i + "] is missing"); 141 } 142 patterns[i] = Pattern.compile(regex, flags); 143 } 144 } 145 146 /** 147 * Gets a copy of the Patterns. 148 * 149 * @return a copy of the Patterns. 150 * @since 1.8 151 */ 152 public Pattern[] getPatterns() { 153 return patterns.clone(); 154 } 155 156 /** 157 * Validates a value against the set of regular expressions. 158 * 159 * @param value The value to validate. 160 * @return {@code true} if the value is valid 161 * otherwise {@code false}. 162 */ 163 public boolean isValid(final String value) { 164 if (value == null) { 165 return false; 166 } 167 for (final Pattern pattern : patterns) { 168 if (pattern.matcher(value).matches()) { 169 return true; 170 } 171 } 172 return false; 173 } 174 175 /** 176 * Validates a value against the set of regular expressions 177 * returning the array of matched groups. 178 * 179 * @param value The value to validate. 180 * @return String array of the <i>groups</i> matched if 181 * valid or {@code null} if invalid 182 */ 183 public String[] match(final String value) { 184 if (value == null) { 185 return null; 186 } 187 for (final Pattern pattern : patterns) { 188 final Matcher matcher = pattern.matcher(value); 189 if (matcher.matches()) { 190 final int count = matcher.groupCount(); 191 final String[] groups = new String[count]; 192 for (int j = 0; j < count; j++) { 193 groups[j] = matcher.group(j + 1); 194 } 195 return groups; 196 } 197 } 198 return null; 199 } 200 201 /** 202 * Provides a String representation of this validator. 203 * @return A String representation of this validator. 204 */ 205 @Override 206 public String toString() { 207 final StringBuilder buffer = new StringBuilder(); 208 buffer.append("RegexValidator{"); 209 for (int i = 0; i < patterns.length; i++) { 210 if (i > 0) { 211 buffer.append(","); 212 } 213 buffer.append(patterns[i].pattern()); 214 } 215 buffer.append("}"); 216 return buffer.toString(); 217 } 218 219 /** 220 * Validates a value against the set of regular expressions 221 * returning a String value of the aggregated groups. 222 * 223 * @param value The value to validate. 224 * @return Aggregated String value comprised of the 225 * <i>groups</i> matched if valid or {@code null} if invalid 226 */ 227 public String validate(final String value) { 228 if (value == null) { 229 return null; 230 } 231 for (final Pattern pattern : patterns) { 232 final Matcher matcher = pattern.matcher(value); 233 if (matcher.matches()) { 234 final int count = matcher.groupCount(); 235 if (count == 1) { 236 return matcher.group(1); 237 } 238 final StringBuilder buffer = new StringBuilder(); 239 for (int j = 0; j < count; j++) { 240 final String component = matcher.group(j + 1); 241 if (component != null) { 242 buffer.append(component); 243 } 244 } 245 return buffer.toString(); 246 } 247 } 248 return null; 249 } 250 251}