ER - 1 Java Regex - Pattern (Java - Util.regex - Pattern)
ER - 1 Java Regex - Pattern (Java - Util.regex - Pattern)
Jenkov.com
By Jakob Jenkov
Connect with me:
Rate article:
13
Share article:
Compartir Tweet 4
[a
Table of Contents
Pattern.matches()
Pattern.compile()
Pattern.matcher()
Pattern.split()
Pattern.pattern()
The Java Pattern class (java.util.regex.Pattern), is the main access point of the Java regular expression
API. Whenever you need to work with regular expressions in Java, you start with Java's Pattern class.
-
Working with regular expressions in Java is also sometimes referred to as pattern matching in Java. A regular
expression is also sometimes referred to as a pattern (hence the name of the Java Pattern class). Thus, the term
pattern matching in Java means matching a regular expression (pattern) against a text using Java.
The Java Pattern class can be used in two ways. You can use the Pattern.matches() method to quickly check if
a text (String) matches a given regular expression. Or you can compile a Pattern instance using
Pattern.compile() which can be used multiple times to match the regular expression against multiple texts. Both
the Pattern.matches() and Pattern.compile() methods are covered below.
Pattern.matches()
The easiest way to check if a regular expression pattern matches a text is to use the static Pattern.matches()
method. Here is a Pattern.matches() example in Java code:
e]
import java.util.regex.Pattern;
String text =
"This is the text to be searched " +
"for occurrences of the pattern.";
Get all my free tips &
tutorials! String pattern = ".*is.*";
Connect with me, or sign up for boolean matches = Pattern.matches(pattern, text);
my news letter or RSS feed,
and get all my tips that help you System.out.println("matches = " + matches);
become a more skilled and }
efficient developer. }
”
This Pattern.matches() example searches the string referenced by the text variable for an occurrence of the
word "is", allowing zero or more characters to be present before and after the word (the two .* parts of the pattern).
Newsletter
First Name * Last Name * The Pattern.matches() method is fine if you just need to check a pattern against a text a single time, and the
default settings of the Pattern class are appropriate.
Email *
http://tutorials.jenkov.com/java-regex/pattern.html#pattern-split Página 1 de 3
Java Regex - Pattern (java.util.regex.Pattern) | tutorials.jenkov.com 08/02/15 9:25 p.m.
Email *
Yes, give me tips! If you need to match for multiple occurrences, and even access the various matches, or just need non-default settings,
you need to compile a Pattern instance using the Pattern.compile() method.
Pattern.compile()
If you need to match a text against a regular expression pattern more than one time, you need to create a Pattern
instance using the Pattern.compile() method. Here is a Java Pattern.compile() example:
import java.util.regex.Pattern;
String text =
"This is the text to be searched " +
"for occurrences of the http:// pattern.";
You can also use the Pattern.compile() method to compile a Pattern using special flags. Here is a Java
Pattern.compile() example using special flags:
The Java Pattern class contains a list of flags (int constants) that you can use to make the Pattern matching
behave in certain ways. The flag used above makes the pattern matching ignore the case of the text when matching.
For more information of the flags you can use with the Java Pattern class, see the JavaDoc for Pattern .
Pattern.matcher()
Once you have obtained a Pattern instance, you can use that to obtain a Matcher instance. The Matcher instance
is used to find matches of the pattern in texts. Here is an example of how to create a Matcher instance from a
Pattern instance:
The Matcher class has a matches() method that tests whether the pattern matches the text. Here is a full example
of how to use the Matcher:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
String text =
"This is the text to be searched " +
"for occurrences of the http:// pattern.";
The Matcher is very advanced, and allows you access to the matched parts of the text in a variety of ways. Too keep
this text short, the Matcher covered in more detail in the text about the Java Matcher class.
Pattern.split()
The split() method in the Pattern class can split a text into an array of String's, using the regular expression
http://tutorials.jenkov.com/java-regex/pattern.html#pattern-split Página 2 de 3
Java Regex - Pattern (java.util.regex.Pattern) | tutorials.jenkov.com 08/02/15 9:25 p.m.
The split() method in the Pattern class can split a text into an array of String's, using the regular expression
(the pattern) as delimiter. Here is a Java Pattern.split() example:
import java.util.regex.Pattern;
String text = "A sep Text sep With sep Many sep Separators";
This Pattern.split() example splits the text in the text variable into 5 separate strings. Each of these strings are
included in the String array returned by the split() method. The parts of the text that matched as delimiters are
not included in the returned String array.
Pattern.pattern()
The pattern() method of the Pattern class simply returns the pattern string (regular expression) that the Pattern
instance was compiled from. Here is an example:
import java.util.regex.Pattern;
In this example the pattern2 variable will contain the value sep, which was the value the Pattern instance was
compiled from.
http://tutorials.jenkov.com/java-regex/pattern.html#pattern-split Página 3 de 3