Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
254 views

Java Regex - Pattern (Java - Util.regex - Pattern) - PDF

The Pattern class in Java is the main access point for working with regular expressions. It provides static and instance methods for compiling patterns, matching strings, and splitting strings. Specifically, Pattern.matches() checks if a string matches a pattern, Pattern.compile() compiles a pattern for multiple uses, Pattern.matcher() returns a Matcher for finding matches, and Pattern.split() splits a string into parts using the pattern.

Uploaded by

Alfredo Canto
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
254 views

Java Regex - Pattern (Java - Util.regex - Pattern) - PDF

The Pattern class in Java is the main access point for working with regular expressions. It provides static and instance methods for compiling patterns, matching strings, and splitting strings. Specifically, Pattern.matches() checks if a string matches a pattern, Pattern.compile() compiles a pattern for multiple uses, Pattern.matcher() returns a Matcher for finding matches, and Pattern.split() splits a string into parts using the pattern.

Uploaded by

Alfredo Canto
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Java Regex - Pattern (java.util.regex.Pattern) | tutorials.jenkov.

com

Jenkov.com

08/02/15 9:25 p.m.

Tutorials

About

Java Charting Tool


100% Java charting components. Application/servlet/JSP integration

Java Regular Expressions

Java Regex - Pattern (java.util.regex.Pattern)

1 Java Regex - Java Regular


Expressions (java.util.regex)

Connect with me:

2 Java Regex - Pattern


(java.util.regex.Pattern)

Rate article:

3 Java Regex - Matcher


(java.util.regex.Matcher)
4 Java Regex - Regular
Expression Syntax

13

By Jakob Jenkov

Share article:
Compartir

Tweet

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:
import java.util.regex.Pattern;
public class PatternMatchesExample {
public static void main(String[] args) {
String text
=
"This is the text to be searched " +
"for occurrences of the pattern.";

Get all my free tips &


tutorials!
Connect with me, or sign up for
my news letter or RSS feed,
and get all my tips that help you
become a more skilled and
efficient developer.

String pattern = ".*is.*";


boolean matches = Pattern.matches(pattern, text);
System.out.println("matches = " + matches);
}
}

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 *
Email *

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.

http://tutorials.jenkov.com/java-regex/pattern.html#pattern-split

Pgina 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;
public class PatternCompileExample {
public static void main(String[] args) {
String text
=
"This is the text to be searched " +
"for occurrences of the http:// pattern.";
String patternString = ".*http://.*";
Pattern pattern = Pattern.compile(patternString);
}
}

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:
Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);

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:
Matcher matcher = pattern.matcher(text);

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;
public class PatternMatcherExample {
public static void main(String[] args) {
String text
=
"This is the text to be searched " +
"for occurrences of the http:// pattern.";
String patternString = ".*http://.*";
Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(text);
boolean matches = matcher.matches();
System.out.println("matches = " + matches);
}
}

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

Pgina 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;
public class PatternSplitExample {
public static void main(String[] args) {
String text = "A sep Text sep With sep Many sep Separators";
String patternString = "sep";
Pattern pattern = Pattern.compile(patternString);
String[] split = pattern.split(text);
System.out.println("split.length = " + split.length);
for(String element : split){
System.out.println("element = " + element);
}
}
}

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;
public class PatternPatternExample {
public static void main(String[] args) {
String patternString = "sep";
Pattern pattern = Pattern.compile(patternString);
String pattern2 = pattern.pattern();
}
}

In this example the pattern2 variable will contain the value sep, which was the value the Pattern instance was
compiled from.

Next: Java Regex - Matcher (java.util.regex.Matcher)


Connect with me:

Newsletter - Get all my free tips!


First Name *

Last Name *

Email *

Yes, give me tips!

http://tutorials.jenkov.com/java-regex/pattern.html#pattern-split

Pgina 3 de 3

You might also like