public class BackReferenceTest extends Object
| Constructor | Description |
|---|---|
BackReferenceTest() |
| Modifier and Type | Method | Description |
|---|---|---|
void |
testBackReferenceRegex() |
The backReference regex:
|
void |
testNamedBackReferenceRegex() |
The namedBackReference regex:
|
public void testBackReferenceRegex()
The backReference regex:
\number
In this case we use the following regex:
(\w)\1
In this test case the backReference matches the expression of the capturing group again. So, with the String "seek" the capturing group, i.e. (\w), matches all the characters but using the backReference matches the same pattern, obtaining the result "ee", because it's the only part of the String that is consecutively equal.
public void testNamedBackReferenceRegex()
The namedBackReference regex:
\k<name>
In this case we use the following regex:
(?<char>\w)\k<char>
There are two parts on this regex, first we define a named capturing group, (?<char>\w), and then we define the
namedBackReference, \k<char>.
This is very similar to the backReference regex. The named capturing group matches all the characters but using the namedBackReference the same pattern is matched, obtaining a single result "ee" using the String "seek", because it's the only part of the String that is consecutively equal.
Copyright © 2018. All rights reserved.