Java in real examples, Searching in text using the Pattern and Matcher Classes

adevedo's picture
0
No votes yet

java.util.regex.Pattern

As per the Javadoc, A compiled representation of a regular expression. A regular expression, specified as a string, must first be compiled into an instance of this class. The resulting pattern can then be used to create a Matcher object that can match arbitrary character sequences against the regular expression. All of the state involved in performing a match resides in the matcher, so many matchers can share the same pattern.

java.util.regex.Matcher

As per the Javadoc, An engine that performs match operations on a character sequence by interpreting a Pattern.

In this article we show some real examples about how to use regular expressions to extract and search for substrings that matches a specific pattern inside long strings

Example 1:

From the project jdk/openjdk / openjdk-6-b14, the class sun.security.util.Debug contains the following sample:

Example 2:

From the project maven2/groovy/groovy / groovy-1.0-jsr-06, the class org.codehaus.groovy.runtime.DefaultGroovyMethods contains the following sample:

Example 3:

From the project maven2/groovy/groovy / groovy-1.0-jsr-06, the class org.codehaus.groovy.runtime.DefaultGroovyMethods contains the following sample:

Example 4:

From the project jdk/openjdk / openjdk-6-b14, the class sun.security.util.Debug contains the following sample: