Regular expression: sequence of characters representing a search pattern, to be used for operations like search and replace.
Regex in Java
When using a regex in a Java application, I suggest to
- build the regexp according needs
- escape the regexp as java String.
See the examples below:
detect an email
The base regex is compound of three blocks:
- account: letters, dots or numbers, representing the user
- the @ character
- provider: letters, dots or numbers, followed by a domain extension (.com, .net, etc.)
The base regexp look like this:
We will need to escape the “slash” and “dot” chars, to use the regex in java. The result is the following:
Usage of Regexp in Java
In java I can compile a regex to get a Pattern object instance, a compiled representation of the regex, built statically to match efficiently input strings.
Pattern p = Patter.compile(“MY-REGEX”);
Matcher m = p.matcher(“STRING-TO-MATCH-WITH-MY-REGEX);
The matched can be used to perform various operations on the String.
- validate: tell if the String is compliant with the regex. As example you can validate user inputs, like an email address, a telephone number, or a postal address
- replace : replace matching text
- group: the regexp might define groups to be used for substring matching.
Below there is the code for a matcher to match the filename extensions for xml, xsd and wsdl files
// matches string end with .xml, .xsd and .wsdl (non case sensitive)
Pattern p = Pattern.compile("^*(\\.(?i)(xml|xsd|wsdl))$");
The advanced features i use more often are
- groups – to match and capture a substring of the input
- non capturing groups – like (?:anything) – to improve performances
some examples you should know
- Java Tutorial Essential – Regular Expressions : link
- Vogella – Java and Regular Expressions : link
- regex101.com – Online Regex Tester : link
- RegexPlanet – Regular Expression Test Page for Java : link
- Optimizing Regex in Java : link
- Mykong – 10 Java Regular expressions you should know : link
- Example Depot – using a non capturing group in Java : link
Java regex matcher : link