java.util.regex.Matcher
java.util.regex.Pattern

Comment Pattern Fixture's fields and methods
regularExpression input replacement ith group() replaceAll() compile() matcher() find() result() appendReplacement() appendTail() groupi()
String String String int String String String String String String String String String



Searching for expressions

Using group() and group(int)

Pattern Fixture
regularExpression
(apple)|(orange)|(peach)

Pattern Fixture
compile?
Pattern Fixture
input
An apple, orange or peach a day will keep the doctor away.


Pattern Fixture
matcher?
group(1) denotes apple. group(4) is undefined because the string has only 3 left parenthesis, so it throws an exception.

Pattern Fixture
find? group? groupi? groupi? groupi? groupi? groupi?
true apple apple apple 1
true orange orange orange 2
true peach peach peach 3
false 4
5




Replacing expressions

Using appendReplacement() and appendTail()

Pattern Fixture
regularExpression input replacement
cat one cat two cats in the yard dog Repace cat with dog

Pattern Fixture
compile? matcher?

Pattern Fixture
appendReplacement? appendReplacement? appendReplacement? appendTail?
one dog


Regular Expression Constructs

This example uses shows how to use regular expressions to find parameters in a SQL script. The syntax for a parameter is

java.util.regex.Pattern contains a full list of constructs for defining regular expressions.


Comment :\s*(((in)?(out)?\s+)?(\d))\b
Characters Matches Example
: : :
\s* Any number of whitespace characters: [ \t\n\x0B\f\r]
( Expression 1 in 1 or inout 2 or out 3 or 4
( Expression 2 - within Expression 1 in or inout or out
( Expression 3 - within Expression 2 in
)? ) -End ofExpression 3.? optional see above
( Expression 4 - within Expression 2 out
)? ) -End ofExpression 4.? optional see above
\s+ There must be at least one space following in/out [ \t\n\x0B\f\r]
)? ) -End ofExpression 2 (the in/out expression).? optional see above
(\d) Expression 5 0 1 2 3 ... 9
\b Word Boundary - Followed by whitespace or end of string Not 123 or 1ABC



Pattern Fixture
regularExpression input compile? matcher?
:\s*(((in)?(out)?\s+)?(\d))\b Begin : out 1 : = : 2 :in 3 :inout 4; end; :inin 5 :outout 6 :77 :8

':inin 5', ':outout6' and ':77' do not have the correct syntax.

Pattern Fixture
find? groupi? groupi? groupi? groupi? groupi? groupi?

Replacing with a Group Reference

Using replaceAll() and $G

replacemenent can refer to the original capturing group with a $G, where G is between 0 and 9.

Call replaceAll() with same Expression
Pattern Fixture
matcher? replacement replaceAll()
$0 Begin : out 1 : = : 2 :in 3 :inout 4; end; :inin 5 :outout 6 :77 :8 Output matches input
:$1 Begin :out 1 : = :2 :in 3 :inout 4; end; :inin 5 :outout 6 :77 :8 Strip spaces after colon
:$5 Begin :1 : = :2 :3 :4; end; :inin 5 :outout 6 :77 :8 Strip spaces and in/out/inout