A Regular Expression is a sequence of characters representing a search pattern, to be used for operations like search and replace.

Standard Language

Regex is a standard used in several programming frameworks and languages such as Perl, Java, Javascript, Python, .NET, W3C XML Schema, etc. Generally, there are only minor differences between languages.

Regex in Java

When using a regex in a Java application, I usually

  1. write the regexp according to my needs
  2. escape the regexp as java String.

Below you can see a few examples

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:

\b[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b

We will need to escape the “slash” and “dot” chars, to use the regex in java. The result is the following:

\\b[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}\\b

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.

Simple example

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))$");

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

adsa


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *