public class AlternationTest extends Object
| Constructor | Description |
|---|---|
AlternationTest() |
| Modifier and Type | Method | Description |
|---|---|---|
void |
testAndRegex() |
The and expression regex:
|
void |
testIfGroupMatchRegex() |
The named conditional expression regex:
|
void |
testIfMatchRegex() |
The conditional expression regex:
|
void |
testOrRegex() |
The or expression regex:
|
public void testAndRegex()
The and expression regex:
&&
In this case we execute the following regex:
[a-z&&[aeiou]]
With this regex it is expected to obtain all the vowels of a String.
public void testOrRegex()
The or expression regex:
|
In this case we execute the following regex:
\d|\s
With this regex it is expected to obtain all the digits and all the white spaces.
public void testIfMatchRegex()
The conditional expression regex:
(?(expression)then|else)
It matches the "expression" and if the expression is matched it applies the "then" expression and if the "expression" isn't matched it applies the "else" expression.
In the test example we have the following expression:
(?(A)A\d{2}\b|\b\d{3}\b)
expression: A
then: A\d{2}\b
else: \b\d{3}\b
This means that this expression will either match an 'A' followed by two digits and a boundary or three digits between boundaries.
public void testIfGroupMatchRegex()
The named conditional expression regex:
(?(name)then|else)
It matches the named capturing group with the name "name", applying the "then" expression if a match occurs or
the "else" expression if it doesn't.
In this test example we have the following expression:
(?<quoted>")?(?(quoted).+?"|\S+\s)
Named capturing group: (?<quoted>")?
name: quoted
then: .+?"
else: \S+\s
There are two distinct aspects, first we define the named capturing group with (?<quoted>")? and then we
use the named conditional expression with (?(quoted).+?"|\S+\s).
In the test example the expression asserts if the "quoted" capturing group occurs and if it does it matches
any String until a '"' char, i.e. the "then" expression, otherwise it matches the remaining text, i.e. the
"else" expression.
Copyright © 2018. All rights reserved.