public class RegexValidator extends Object implements Serializable
Construct 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);
true
or false
:
boolean valid = validator.isValid(value);
String result = validator.validate(value);
String[] result = validator.match(value);
Note that patterns are matched against the entire input.
Cached instances pre-compile and re-use Pattern
(s) - which according to the Pattern
API are safe to use in a multi-threaded environment.
Constructor and Description |
---|
RegexValidator(String regex)
Construct a case sensitive validator for a single regular expression.
|
RegexValidator(String[] regexs)
Construct a case sensitive validator that matches any one of the set of regular
expressions.
|
RegexValidator(String[] regexs,
boolean caseSensitive)
Construct a validator that matches any one of the set of regular expressions with the specified
case sensitivity.
|
RegexValidator(String regex,
boolean caseSensitive)
Construct a validator for a single regular expression with the specified case sensitivity.
|
Modifier and Type | Method and Description |
---|---|
boolean |
isValid(String value)
Validate a value against the set of regular expressions.
|
String[] |
match(String value)
Validate a value against the set of regular expressions returning the array of matched groups.
|
String |
toString()
Provide a String representation of this validator.
|
String |
validate(String value)
Validate a value against the set of regular expressions returning a String value of the
aggregated groups.
|
public RegexValidator(String regex)
regex
- The regular expression this validator will validate againstpublic RegexValidator(String regex, boolean caseSensitive)
regex
- The regular expression this validator will validate againstcaseSensitive
- when true
matching is case sensitive, otherwise
matching is case in-sensitivepublic RegexValidator(String[] regexs)
regexs
- The set of regular expressions this validator will validate againstpublic RegexValidator(String[] regexs, boolean caseSensitive)
regexs
- The set of regular expressions this validator will validate againstcaseSensitive
- when true
matching is case sensitive, otherwise
matching is case in-sensitivepublic boolean isValid(String value)
value
- The value to validate.true
if the value is valid otherwise false
.public String[] match(String value)
value
- The value to validate.null
if invalidpublic String validate(String value)
value
- The value to validate.null
if invalid