Regular expression: sequence of characters representing a search pattern, to be used for operations like search and replace.

Standard Language

Regex, as we know them, are a standard adopted in many programming frameworks and languages, such as: Perl, Java, Javascript, Python, .NET, W3C XML Schema, etc. Generally there are only minos differences between languages.

Regex in Java

When using a regex in a Java application, I suggest to

  1. build the regexp according needs
  2. 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.

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.

 

Simple example

Below there is the code for a matcher to match the filename extensions for xml, xsd and wsdl files

 

Advanced features

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

see: http://www.exampledepot.com/egs/java.util.regex/NoGroup.html

some examples you should know

http://www.mkyong.com/regular-expressions/10-java-regular-expression-examples-you-should-know/

 

References

  • 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