public class GroupingTest extends Object
| Constructor | Description |
|---|---|
GroupingTest() |
| Modifier and Type | Method | Description |
|---|---|---|
void |
testExpressionsWithOptionsRegex() |
The expressionWithOptions regex:
|
void |
testNamedSubExpressionRegex() |
The namedSubExpression regex:
|
void |
testNegativeLookAheadRegex() |
The negativeLookAhead regex:
|
void |
testNegativeLookBehindRegex() |
The negativeLookBehind regex:
|
void |
testNonBacktrackingRegex() |
The nonBackTracking regex:
|
void |
testNonCapturingGroupRegex() |
The nonCapturingGroup regex:
|
void |
testPositiveLookAheadRegex() |
The positiveLookAhead regex:
|
void |
testPositiveLookBehindRegex() |
The positiveLookBehind regex:
|
void |
testSubExpressionRegex() |
The subExpression regex:
|
public void testSubExpressionRegex()
The subExpression regex:
(subExpr)
In this example we use the following regex:
(\w)\1
We define a group using the "(\w)" and use a backReference after. This will match the \w two times. Using a String "deep" the result should be "ee".
public void testNamedSubExpressionRegex()
The namedSubExpression regex:
(?<name>subExpr)
In this example we use the following regex:
(?<double>\w)\k<double>
This regex is divided in two aspects, first we define the namedSubExpression, i.e. (?<double>\w), and then use a
namedBackReference. This will result in the pattern, \w, being matched 2 consecutive times. Using a String
"deep" the result should be "ee".
public void testNonCapturingGroupRegex()
The nonCapturingGroup regex:
(?:subExpr)
In this example we use the following regex:
Write(?:Line)
This will match any String containing "Write" or "WriteLine" since the "Line" is optional since it's defined inside a non capturing group.
public void testExpressionsWithOptionsRegex()
The expressionWithOptions regex:
(?imnsx-imnsx:subExpr)
In this example we use the following regex:
A\d{2}(?i:\w+)\b
This expression will match any 'A' character followed by two digits then followed by a case insensitive, i.e. the 'i' option, group of letters or digits. Using the String "A12xl A12XL a12xl" we will have two matches:
1: A12xl
2: A12XL
public void testPositiveLookAheadRegex()
The positiveLookAhead regex:
(?=subExpr)
In this example we use the following regex:
\w+(?=\.)
This matches any group of letters or digits that are followed by a '.' character.
public void testNegativeLookAheadRegex()
The negativeLookAhead regex:
(?!subExpr)
In this example we use the following regex:
\b(?!un)\w+\b
This matches group of letters or digits that don't have the "un" prefix that exist between boundaries. Using the String "unsure sure unity used" we obtain 2 results:
1: sure - from sure
2: used - from used
Both unsure and unity aren't eligible for results since they are prefix with "un".
public void testPositiveLookBehindRegex()
The positiveLookBehind regex:
(?<=subExpr)
In this example we use the following regex:
(?<=19)\d{2}\b
This matches any two numbers that are prefixed with "19". Using the String "1851 1999 1950 1905 2003" the results are:
1: 99 - from 1999
2: 50 - from 1950
3: 05 - from 1905
public void testNegativeLookBehindRegex()
The negativeLookBehind regex:
(?<!subExpr)
In this example we use the following regex:
(?<!19)\d{2}\b
This matches any two numbers that aren't prefixed with "19". Using the String "1851 1999 1950 1905 2003" the results are:
1: 51 - from 1851
2: 03 - from 2003
public void testNonBacktrackingRegex()
The nonBackTracking regex:
(?>subExpr)
In this example we use the following regex:
[13579](?>A+B+)
This will match any '1', '3', '5', '7', '9' character followed by multiple 'A' characters then followed by multiple 'B' characters. Using the String "1ABB 3ABBC 5AB 5AC" the results should be:
1: 1ABB
2: 3ABB
3: 5AB
Copyright © 2018. All rights reserved.