Package ru.lanwen.verbalregex
Class VerbalExpression.Builder
- java.lang.Object
-
- ru.lanwen.verbalregex.VerbalExpression.Builder
-
- Enclosing class:
- VerbalExpression
public static class VerbalExpression.Builder extends java.lang.Object
-
-
Field Summary
Fields Modifier and Type Field Description private int
modifiers
private java.lang.StringBuilder
prefixes
private java.lang.StringBuilder
source
private java.lang.StringBuilder
suffixes
private static java.util.Map<java.lang.Character,java.lang.Integer>
SYMBOL_MAP
-
Constructor Summary
Constructors Constructor Description Builder()
Package private.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description VerbalExpression.Builder
add(java.lang.String pValue)
Append literal expression Everything added to the expression should go trough this method (keep in mind when creating your own methods).VerbalExpression.Builder
add(VerbalExpression.Builder regex)
Append a regex from builder and wrap it with unnamed group (?: ...VerbalExpression.Builder
addModifier(char pModifier)
VerbalExpression.Builder
any(java.lang.String value)
Shortcut toanyOf(String)
VerbalExpression.Builder
anyOf(java.lang.String pValue)
VerbalExpression.Builder
anything()
Add expression that matches anything (includes empty string)VerbalExpression.Builder
anythingBut(java.lang.String pValue)
Add expression that matches anything, but not passed argumentVerbalExpression.Builder
atLeast(int from)
Produce range count with only minimal number of occurrences for example: .find("w").atLeast(1) // produce (?:w){1,}VerbalExpression.Builder
br()
Shortcut forlineBreak()
VerbalExpression
build()
VerbalExpression.Builder
capt()
Shortcut forcapture()
VerbalExpression.Builder
capt(java.lang.String name)
Shortcut forcapture(String)
VerbalExpression.Builder
capture()
Adds capture - open brace to current position and closed to suffixesVerbalExpression.Builder
capture(java.lang.String name)
Adds named-capture - open brace to current position and closed to suffixesVerbalExpression.Builder
count(int count)
Add count of previous group for example: .find("w").count(3) // produce - (?:w){3}VerbalExpression.Builder
count(int from, int to)
Produce range count for example: .find("w").count(1, 3) // produce (?:w){1,3}private int
countOccurrencesOf(java.lang.String where, java.lang.String what)
Counts occurrences of some substring in whole string Same as org.apache.commons.lang3.StringUtils#countMatches(String, java.lang.String) by effect.VerbalExpression.Builder
digit()
Add same as [0-9]VerbalExpression.Builder
endCapt()
Shortcut forendCapture()
VerbalExpression.Builder
endCapture()
Close brace for previous capture and remove last closed brace from suffixes Can be used to continue build regex after capture or to add multiply capturesVerbalExpression.Builder
endGr()
Closes current unnamed and unmatching group Shortcut forendCapture()
Use it withgroup()
for prettify code Example: regex().group().maybe("word").count(2).endGr()VerbalExpression.Builder
endOfLine()
Mark the expression to end at the last character of the line Same asendOfLine(boolean)
with true argVerbalExpression.Builder
endOfLine(boolean pEnable)
Enable or disable the expression to end at the last character of the lineVerbalExpression.Builder
find(java.lang.String value)
Add a string to the expression Syntax sugar forthen(String)
- use it in case: regex().find("string") // when it goes firstVerbalExpression.Builder
group()
Same ascapture()
, but don't save result May be used to set count of duplicated captures, without creating a new saved capture Example: // Without group() - count(2) applies only to second capture regex().group() .capt().range("0", "1").endCapt().tab() .capt().digit().count(5).endCapt() .endGr().count(2);VerbalExpression.Builder
lineBreak()
Add universal line break expressionVerbalExpression.Builder
maybe(java.lang.String pValue)
Add a string to the expression that might appear once (or not) Example: The following matches all strings that contain http:// or https:// VerbalExpression regex = regex() .find("http") .maybe("s") .then("://") .anythingBut(" ").build(); regex.test("http://") //true regex.test("https://") //trueVerbalExpression.Builder
maybe(VerbalExpression.Builder regex)
Add a regex to the expression that might appear once (or not) Example: The following matches all names that have a prefix or not.VerbalExpression.Builder
multiple(java.lang.String pValue, int... count)
Convenient method to show that string usage count is exact count, range count or simply one or more Usage: regex().multiply("abc") // Produce (?:abc)+ regex().multiply("abc", null) // Produce (?:abc)+ regex().multiply("abc", (int)from) // Produce (?:abc){from} regex().multiply("abc", (int)from, (int)to) // Produce (?:abc){from, to} regex().multiply("abc", (int)from, (int)to, (int)...) // Produce (?:abc)+VerbalExpression.Builder
nonDigit()
Add non-digit: [^0-9]VerbalExpression.Builder
nonSpace()
Add non-whitespace character: [^\s]VerbalExpression.Builder
nonWordChar()
Add non-word character: [^\w]VerbalExpression.Builder
oneOf(java.lang.String... pValues)
Adds an alternative expression to be matched based on an array of valuesVerbalExpression.Builder
oneOrMore()
Adds "+" char to regexp Same effect asatLeast(int)
with "1" argument Also, used bymultiple(String, int...)
when second argument is null, or have length more than 2VerbalExpression.Builder
or(java.lang.String pValue)
Add a alternative expression to be matched Issue #32VerbalExpression.Builder
range(java.lang.String... pArgs)
Add expression to match a range (or multiply ranges) Usage: .range(from, to [, from, to ...VerbalExpression.Builder
removeModifier(char pModifier)
private java.lang.String
sanitize(java.lang.String pValue)
Escapes any non-word char with two backslashes used by any method, exceptadd(String)
VerbalExpression.Builder
searchOneLine(boolean pEnable)
VerbalExpression.Builder
something()
Add expression that matches something that might appear once (or more)VerbalExpression.Builder
somethingButNot(java.lang.String pValue)
VerbalExpression.Builder
space()
Add whitespace character, same as [ \t\n\x0B\f\r]VerbalExpression.Builder
startOfLine()
Mark the expression to start at the beginning of the line Same asstartOfLine(boolean)
with true argVerbalExpression.Builder
startOfLine(boolean pEnable)
Enable or disable the expression to start at the beginning of the lineVerbalExpression.Builder
tab()
Add expression to match a tab character (' ')VerbalExpression.Builder
then(java.lang.String pValue)
Add a string to the expressionVerbalExpression.Builder
withAnyCase()
Turn ON matching with ignoring case Example: // matches "a" // matches "A" regex().find("a").withAnyCase()VerbalExpression.Builder
withAnyCase(boolean pEnable)
VerbalExpression.Builder
word()
Add word, same as [a-zA-Z_0-9]+VerbalExpression.Builder
wordBoundary()
Add word boundary: \bVerbalExpression.Builder
wordChar()
Add word character, same as [a-zA-Z_0-9]VerbalExpression.Builder
zeroOrMore()
Adds "*" char to regexp, means zero or more times repeated Same effect asatLeast(int)
with "0" argument
-
-
-
Constructor Detail
-
Builder
Builder()
Package private. UseVerbalExpression.regex()
to build a new one- Since:
- 1.2
-
-
Method Detail
-
sanitize
private java.lang.String sanitize(java.lang.String pValue)
Escapes any non-word char with two backslashes used by any method, exceptadd(String)
- Parameters:
pValue
- - the string for char escaping- Returns:
- sanitized string value
-
countOccurrencesOf
private int countOccurrencesOf(java.lang.String where, java.lang.String what)
Counts occurrences of some substring in whole string Same as org.apache.commons.lang3.StringUtils#countMatches(String, java.lang.String) by effect. Used to count braces foror(String)
method- Parameters:
where
- - where to findwhat
- - what needs to count matches- Returns:
- 0 if nothing found, count of occurrences instead
-
build
public VerbalExpression build()
-
add
public VerbalExpression.Builder add(java.lang.String pValue)
Append literal expression Everything added to the expression should go trough this method (keep in mind when creating your own methods). All existing methods already use this, so for basic usage, you can just ignore this method. Example: regex().add("\n.*").build() // produce exact "\n.*" regexp- Parameters:
pValue
- - literal expression, not sanitized- Returns:
- this builder
-
add
public VerbalExpression.Builder add(VerbalExpression.Builder regex)
Append a regex from builder and wrap it with unnamed group (?: ... )- Parameters:
regex
- - VerbalExpression.Builder, that not changed- Returns:
- this builder
- Since:
- 1.2
-
startOfLine
public VerbalExpression.Builder startOfLine(boolean pEnable)
Enable or disable the expression to start at the beginning of the line- Parameters:
pEnable
- - enables or disables the line starting- Returns:
- this builder
-
startOfLine
public VerbalExpression.Builder startOfLine()
Mark the expression to start at the beginning of the line Same asstartOfLine(boolean)
with true arg- Returns:
- this builder
-
endOfLine
public VerbalExpression.Builder endOfLine(boolean pEnable)
Enable or disable the expression to end at the last character of the line- Parameters:
pEnable
- - enables or disables the line ending- Returns:
- this builder
-
endOfLine
public VerbalExpression.Builder endOfLine()
Mark the expression to end at the last character of the line Same asendOfLine(boolean)
with true arg- Returns:
- this builder
-
then
public VerbalExpression.Builder then(java.lang.String pValue)
Add a string to the expression- Parameters:
pValue
- - the string to be looked for (sanitized)- Returns:
- this builder
-
find
public VerbalExpression.Builder find(java.lang.String value)
Add a string to the expression Syntax sugar forthen(String)
- use it in case: regex().find("string") // when it goes first- Parameters:
value
- - the string to be looked for (sanitized)- Returns:
- this builder
-
maybe
public VerbalExpression.Builder maybe(java.lang.String pValue)
Add a string to the expression that might appear once (or not) Example: The following matches all strings that contain http:// or https:// VerbalExpression regex = regex() .find("http") .maybe("s") .then("://") .anythingBut(" ").build(); regex.test("http://") //true regex.test("https://") //true- Parameters:
pValue
- - the string to be looked for- Returns:
- this builder
-
maybe
public VerbalExpression.Builder maybe(VerbalExpression.Builder regex)
Add a regex to the expression that might appear once (or not) Example: The following matches all names that have a prefix or not. VerbalExpression.Builder namePrefix = regex().oneOf("Mr.", "Ms."); VerbalExpression name = regex() .maybe(namePrefix) .space() .zeroOrMore() .word() .oneOrMore() .build(); regex.test("Mr. Bond/") //true regex.test("James") //true- Parameters:
regex
- - the string to be looked for- Returns:
- this builder
-
anything
public VerbalExpression.Builder anything()
Add expression that matches anything (includes empty string)- Returns:
- this builder
-
anythingBut
public VerbalExpression.Builder anythingBut(java.lang.String pValue)
Add expression that matches anything, but not passed argument- Parameters:
pValue
- - the string not to match- Returns:
- this builder
-
something
public VerbalExpression.Builder something()
Add expression that matches something that might appear once (or more)- Returns:
- this builder
-
somethingButNot
public VerbalExpression.Builder somethingButNot(java.lang.String pValue)
-
lineBreak
public VerbalExpression.Builder lineBreak()
Add universal line break expression- Returns:
- this builder
-
br
public VerbalExpression.Builder br()
Shortcut forlineBreak()
- Returns:
- this builder
-
tab
public VerbalExpression.Builder tab()
Add expression to match a tab character (' ')- Returns:
- this builder
-
word
public VerbalExpression.Builder word()
Add word, same as [a-zA-Z_0-9]+- Returns:
- this builder
-
wordChar
public VerbalExpression.Builder wordChar()
Add word character, same as [a-zA-Z_0-9]- Returns:
- this builder
-
nonWordChar
public VerbalExpression.Builder nonWordChar()
Add non-word character: [^\w]- Returns:
- this builder
-
nonDigit
public VerbalExpression.Builder nonDigit()
Add non-digit: [^0-9]- Returns:
- this builder
-
digit
public VerbalExpression.Builder digit()
Add same as [0-9]- Returns:
- this builder
-
space
public VerbalExpression.Builder space()
Add whitespace character, same as [ \t\n\x0B\f\r]- Returns:
- this builder
-
nonSpace
public VerbalExpression.Builder nonSpace()
Add non-whitespace character: [^\s]- Returns:
- this builder
-
wordBoundary
public VerbalExpression.Builder wordBoundary()
Add word boundary: \bExample:
VerbalExpression regex = regex() .wordBoundary().find("abc").wordBoundary() .build(); regex.test("a abc"); // true regex.test("a.abc"); // true regex.test("aabc"); // false
- Returns:
- this builder
-
anyOf
public VerbalExpression.Builder anyOf(java.lang.String pValue)
-
any
public VerbalExpression.Builder any(java.lang.String value)
Shortcut toanyOf(String)
- Parameters:
value
- - CharSequence every char from can be matched- Returns:
- this builder
-
range
public VerbalExpression.Builder range(java.lang.String... pArgs)
Add expression to match a range (or multiply ranges) Usage: .range(from, to [, from, to ... ]) Example: The following matches a hexadecimal number: regex().range( "0", "9", "a", "f") // produce [0-9a-f]- Parameters:
pArgs
- - pairs for range- Returns:
- this builder
-
addModifier
public VerbalExpression.Builder addModifier(char pModifier)
-
removeModifier
public VerbalExpression.Builder removeModifier(char pModifier)
-
withAnyCase
public VerbalExpression.Builder withAnyCase(boolean pEnable)
-
withAnyCase
public VerbalExpression.Builder withAnyCase()
Turn ON matching with ignoring case Example: // matches "a" // matches "A" regex().find("a").withAnyCase()- Returns:
- this builder
-
searchOneLine
public VerbalExpression.Builder searchOneLine(boolean pEnable)
-
multiple
public VerbalExpression.Builder multiple(java.lang.String pValue, int... count)
Convenient method to show that string usage count is exact count, range count or simply one or more Usage: regex().multiply("abc") // Produce (?:abc)+ regex().multiply("abc", null) // Produce (?:abc)+ regex().multiply("abc", (int)from) // Produce (?:abc){from} regex().multiply("abc", (int)from, (int)to) // Produce (?:abc){from, to} regex().multiply("abc", (int)from, (int)to, (int)...) // Produce (?:abc)+- Parameters:
pValue
- - the string to be looked forcount
- - (optional) if passed one or two numbers, it used to show count or range count- Returns:
- this builder
- See Also:
oneOrMore()
,then(String)
,zeroOrMore()
-
oneOrMore
public VerbalExpression.Builder oneOrMore()
Adds "+" char to regexp Same effect asatLeast(int)
with "1" argument Also, used bymultiple(String, int...)
when second argument is null, or have length more than 2- Returns:
- this builder
- Since:
- 1.2
-
zeroOrMore
public VerbalExpression.Builder zeroOrMore()
Adds "*" char to regexp, means zero or more times repeated Same effect asatLeast(int)
with "0" argument- Returns:
- this builder
- Since:
- 1.2
-
count
public VerbalExpression.Builder count(int count)
Add count of previous group for example: .find("w").count(3) // produce - (?:w){3}- Parameters:
count
- - number of occurrences of previous group in expression- Returns:
- this Builder
-
count
public VerbalExpression.Builder count(int from, int to)
Produce range count for example: .find("w").count(1, 3) // produce (?:w){1,3}- Parameters:
from
- - minimal number of occurrencesto
- - max number of occurrences- Returns:
- this Builder
- See Also:
count(int)
-
atLeast
public VerbalExpression.Builder atLeast(int from)
Produce range count with only minimal number of occurrences for example: .find("w").atLeast(1) // produce (?:w){1,}- Parameters:
from
- - minimal number of occurrences- Returns:
- this Builder
- Since:
- 1.2
- See Also:
count(int)
,oneOrMore()
,zeroOrMore()
-
or
public VerbalExpression.Builder or(java.lang.String pValue)
Add a alternative expression to be matched Issue #32- Parameters:
pValue
- - the string to be looked for- Returns:
- this builder
-
oneOf
public VerbalExpression.Builder oneOf(java.lang.String... pValues)
Adds an alternative expression to be matched based on an array of values- Parameters:
pValues
- - the strings to be looked for- Returns:
- this builder
- Since:
- 1.3
-
capture
public VerbalExpression.Builder capture()
Adds capture - open brace to current position and closed to suffixes- Returns:
- this builder
-
capture
public VerbalExpression.Builder capture(java.lang.String name)
Adds named-capture - open brace to current position and closed to suffixesExample:
String text = "test@example.com"; VerbalExpression regex = regex() .find("@") .capture("domain").anything().build(); regex.getText(text, "domain"); // => "example.com"
- Returns:
- this builder
- Since:
- 1.6
-
capt
public VerbalExpression.Builder capt()
Shortcut forcapture()
- Returns:
- this builder
- Since:
- 1.2
-
capt
public VerbalExpression.Builder capt(java.lang.String name)
Shortcut forcapture(String)
- Returns:
- this builder
- Since:
- 1.6
-
group
public VerbalExpression.Builder group()
Same ascapture()
, but don't save result May be used to set count of duplicated captures, without creating a new saved capture Example: // Without group() - count(2) applies only to second capture regex().group() .capt().range("0", "1").endCapt().tab() .capt().digit().count(5).endCapt() .endGr().count(2);- Returns:
- this builder
- Since:
- 1.2
-
endCapture
public VerbalExpression.Builder endCapture()
Close brace for previous capture and remove last closed brace from suffixes Can be used to continue build regex after capture or to add multiply captures- Returns:
- this builder
-
endCapt
public VerbalExpression.Builder endCapt()
Shortcut forendCapture()
- Returns:
- this builder
- Since:
- 1.2
-
endGr
public VerbalExpression.Builder endGr()
Closes current unnamed and unmatching group Shortcut forendCapture()
Use it withgroup()
for prettify code Example: regex().group().maybe("word").count(2).endGr()- Returns:
- this builder
- Since:
- 1.2
-
-