Examples using the Pattern class

Let's look at a few examples to understand some of these methods.

To compile a regular expression for decimal numbers, we can use the following code snippet:

final String decimalPattern = "^[+-]?\d*\.?\d+$"; 
Final Pattern pattern = Pattern.compile(decimalPattern);

The static method, Pattern.compile, compiles a string regex and returns a Pattern instance.

To match text between ## and ## that may include newlines as well, we can use the following compiled pattern:

final String re = "##.*?##"; 
Final Pattern pattern = Pattern.compile(re, Pattern.DOTALL);

Here, we are using two parameters: the Pattern.compile method and passing DOTALL as a flag in the second parameter, since we want to match the newline as well as using our lazy pattern .*?.

Note the use of lazy pattern .*? instead of greedy .* so that we match the shortest match between ## and ##.

We can also write the preceding code snippet using the inline mode modifier, (?s):

final String re = "(?s)##.*?##"; 
Final Pattern pattern = Pattern.compile(re);

If we want to match a string that contains a subsequence, +-*/., surrounded by one or more white spaces on both the sides, then we can use the following code:

package example.regex; 

import java.util.*;
import java.util.regex.*;

class PatternQuoteExample
{
public static void main (String[] args)
{
String input = "Math operators: +-*/. ";
boolean result;

String quoted = Pattern.quote("+-*/.");
System.out.println(quoted);

// regex using standard escaping
result = input.matches(".*\s+\+-\*/\.\s+.*");

System.out.println(result);

// regex Using Pattern.quote around our search string
result = input.matches(".*\s+" + quoted + "\s+.*");

System.out.println(result);

// regex Using Q and E around our search string
result = input.matches(".*\s+\Q+-*/.\E\s+.*");

System.out.println(result);

}
}

After compiling and running this code, will quoted string as: "Q+-*/.E" and then print true for all the three cases as the call to matches succeeds all the times. However, an important difference is the use of Pattern.quote in the second case, which handles the quoting of special regex characters in the search string, such as +, *, .

Then, in the third case, we just wrap our search string using \Q and \E, which is the same as calling Pattern.quote with our search string.

To split an input text on two pipes or ||, we can use following code:

package example.regex; 

import java.util.*;
import java.util.regex.*;

class PatternSplitExample
{
public static void main (String[] args)
{
final String input = "value1||value2||value3";
final Pattern p = Pattern.compile(Pattern.quote("||"));

// call split and print each element from generated array
// using stream API
Arrays.stream(p.split(input))
.forEach(System.out::println);
}
}

Consider the following few points about this code:

  • We call Pattern.quote to avoid escaping double pipe string
  • We call Pattern.compile to compile our string regex and get back a compiled Pattern object
  • We use a generated pattern instance to call the split method by supplying an input string that we want to operate on

Java 8 added a new method, splitAsStream, which returns a stream containing the substring from the given input sequence around the matches of this pattern. Using splitAsStream, we can simplify the preceding class as follows:

package example.regex; 

import java.util.*;
import java.util.regex.*;

class PatternSplitStreamExample
{
public static void main (String[] args) throws java.lang.Exception
{
final String input = "value1||value2||value3";
final Pattern p = Pattern.compile(Pattern.quote("||"));

// call splitAsStream and print each element from generated stream
p.splitAsStream(input)
.forEach(System.out::println);
}
}

Note the use of the splitAsStream method instead of the Arrays.stream() static method in this class. Creating an array performs the whole split. When Pattern returns a stream, it can do the splitting only when it is needed. If we, for example, limit the stream to work up only the first 10 elements, then the splitting does not need to do the splitting for further elements.

It is true even if some of the implementations just do the splitting and return an array based stream from splitAsStream(). A different implementation of the JDK is free to use a better solution only if we use splitAsStream() but has no choice if we use split() and convert to stream afterward.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.253.197