public class AnchorTest extends Object
| Constructor | Description |
|---|---|
AnchorTest() |
| Modifier and Type | Method | Description |
|---|---|---|
void |
testAtBeginningRegex() |
The atBeginning regex:
|
void |
testAtEndRegex() |
The atEnd regex:
|
void |
testAtStringBeginningRegex() |
The atStringBeginning regex:
|
void |
testAtStringEndOrNewlineRegex() |
The atStringEndOrNewLine regex:
|
void |
testAtStringEndRegex() |
The atStringEnd regex:
|
void |
testBoundariesRegex() |
The boundary regex:
|
void |
testConsecutiveMatchRegex() |
The consecutiveMatch regex:
|
void |
testNonBoundariesRegex() |
The noBoundary regex:
|
public void testAtBeginningRegex()
The atBeginning regex:
^
In this case we execute the following regex:
^\d{3}
This should match any three digits at the beginning of a String. As a result we can see that it obtains "901" as a result and not "333" since it's not at the beginning of the String.
public void testAtEndRegex()
The atEnd regex:
$
In this case we execute the following regex:
-\d{3}$
This should match any '-' followed by three digits at the end of a String. As a result we can see that it obtains "-333" as a result and not "-901" since it's not at the end of the String.
public void testAtStringBeginningRegex()
The atStringBeginning regex:
\A
In this case we execute the following regex:
\A\d{3}
This one is identical to the atBeginning regex (^), it matches the expression only if it occurs at the start of the String. As a result we can see that it obtains "901" as a result and not "333" since it's not at the beginning of the String.
public void testAtStringEndOrNewlineRegex()
The atStringEndOrNewLine regex:
\Z
In this case we execute the following regex:
-\d{3}\Z
This should match any '-' character followed by three digits either before the end of the String or before a new line character , i.e. '\n'. As a result we can see that with the two distinct variants, a String ending or the use of the new line character the result is the same with the same regex.
public void testAtStringEndRegex()
The atStringEnd regex:
\z
In this case we execute the following regex:
-\d{3}\z
This should match any '-' character followed by three digits before the end of the line. Contrary to the atStringOrNewLine regex, i.e. "\z", it doesn't match results followed by a new line character, i.e. '\n'. This behaviour is confirmed by the usage of two distinct variants of the match String.
public void testConsecutiveMatchRegex()
The consecutiveMatch regex:
\G
In this case we execute the following regex:
\G\(\d\)
This should match any consecutive match of occurrences of the '(' character followed by any digit which is then followed by a ')' character.
In the test case we use the String "(1)(3)(5)[7](9)" as input. In this case we have four occurrences of matches to the \(\d\) pattern
1: (1)
2: (3)
3: (5)
4: (9)
But only the first three, i.e. (1), (3) and (5), happen consecutively, so only those are matched by the used pattern.
public void testBoundariesRegex()
The boundary regex:
\b
In this case we execute two distinct regex:
regex1: \b\d\b
regex2: \b\d
String used: 5+7=12
Boundary definition: The \b anchor specifies that the match must occur on a boundary between a word character (the \w language element) and a non-word character (the \W language element).
To understand how boundaries work we choose those two examples. In the first case the digit should be between boundaries on both sides. In this case we have two matches, '5' and '7' because both of them are surrounded by boundaries. On the second case we remove the requirement of the right boundary, resulting in having one more result, '1', due to '2' not being boundary character.
public void testNonBoundariesRegex()
The noBoundary regex:
\B
In this case we have two distinct regex:
regex1: \B\d\B
regex2: \b\d
String used: 5+7=12
The noBoundary regex is the opposite of the boundary regex. In the first case we have no results since all digits have at least one boundary next to them. In the second case we removed the noBoundary premise to the right of the digit and obtained a match, '2', since '2' already had a noBoundary character to the left, '1' but was still limited by the boundary that had to the right, the end of the String.
Copyright © 2018. All rights reserved.