Class RegexValidator
java.lang.Object
org.apache.commons.validator.routines.RegexValidator
- All Implemented Interfaces:
Serializable
Regular Expression validation (using the JRE's regular expression support).
Constructs the validator either for a single regular expression or a set (array) of regular expressions. By default validation is case sensitive but constructors are provided to allow case in-sensitive validation. For example to create a validator which does case in-sensitive validation for a set of regular expressions:
String[] regexs = new String[] {...};
RegexValidator validator = new RegexValidator(regexs, false);
- Validate
true
orfalse
: -
boolean valid = validator.isValid(value);
- Validate returning an aggregated String of the matched groups:
-
String result = validator.validate(value);
- Validate returning the matched groups:
-
String[] result = validator.match(value);
Cached instances pre-compile and re-use Pattern
(s) - which according
to the Pattern
API are safe to use in a multi-threaded environment.
- Since:
- 1.4
- See Also:
-
Constructor Summary
ConstructorDescriptionRegexValidator
(String regex) Constructs a case sensitive validator for a single regular expression.RegexValidator
(String... regexs) Constructs a case sensitive validator that matches any one in the array of regular expressions.RegexValidator
(String[] regexs, boolean caseSensitive) Constructs a validator that matches any one of the set of regular expressions with the specified case sensitivity.RegexValidator
(String regex, boolean caseSensitive) Constructs a validator for a single regular expression with the specified case sensitivity. -
Method Summary
Modifier and TypeMethodDescriptionPattern[]
Gets a copy of the Patterns.boolean
Validates a value against the set of regular expressions.String[]
Validates a value against the set of regular expressions returning the array of matched groups.toString()
Provides a String representation of this validator.Validates a value against the set of regular expressions returning a String value of the aggregated groups.
-
Constructor Details
-
RegexValidator
Constructs a case sensitive validator for a single regular expression.- Parameters:
regex
- The regular expression this validator will validate against
-
RegexValidator
Constructs a case sensitive validator that matches any one in the array of regular expressions.- Parameters:
regexs
- The set of regular expressions this validator will validate against
-
RegexValidator
Constructs a validator for a single regular expression with the specified case sensitivity.- Parameters:
regex
- The regular expression this validator will validate againstcaseSensitive
- whentrue
matching is case sensitive, otherwise matching is case in-sensitive
-
RegexValidator
Constructs a validator that matches any one of the set of regular expressions with the specified case sensitivity.- Parameters:
regexs
- The set of regular expressions this validator will validate againstcaseSensitive
- whentrue
matching is case sensitive, otherwise matching is case in-sensitive
-
-
Method Details
-
getPatterns
Gets a copy of the Patterns.- Returns:
- a copy of the Patterns.
- Since:
- 1.8
-
isValid
Validates a value against the set of regular expressions.- Parameters:
value
- The value to validate.- Returns:
true
if the value is valid otherwisefalse
.
-
match
Validates a value against the set of regular expressions returning the array of matched groups.- Parameters:
value
- The value to validate.- Returns:
- String array of the groups matched if
valid or
null
if invalid
-
toString
Provides a String representation of this validator. -
validate
Validates a value against the set of regular expressions returning a String value of the aggregated groups.- Parameters:
value
- The value to validate.- Returns:
- Aggregated String value comprised of the
groups matched if valid or
null
if invalid
-