Package ru.lanwen.verbalregex
Class VerbalExpression.Builder
java.lang.Object
ru.lanwen.verbalregex.VerbalExpression.Builder
- Enclosing class:
VerbalExpression
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate int
private StringBuilder
private StringBuilder
private StringBuilder
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAppend literal expression Everything added to the expression should go trough this method (keep in mind when creating your own methods).add
(VerbalExpression.Builder regex) Append a regex from builder and wrap it with unnamed group (?: ...addModifier
(char pModifier) Shortcut toanyOf(String)
anything()
Add expression that matches anything (includes empty string)anythingBut
(String pValue) Add expression that matches anything, but not passed argumentatLeast
(int from) Produce range count with only minimal number of occurrences for example: .find("w").atLeast(1) // produce (?:w){1,}br()
Shortcut forlineBreak()
build()
capt()
Shortcut forcapture()
Shortcut forcapture(String)
capture()
Adds capture - open brace to current position and closed to suffixesAdds named-capture - open brace to current position and closed to suffixescount
(int count) Add count of previous group for example: .find("w").count(3) // produce - (?:w){3}count
(int from, int to) Produce range count for example: .find("w").count(1, 3) // produce (?:w){1,3}private int
countOccurrencesOf
(String where, String what) Counts occurrences of some substring in whole string Same as org.apache.commons.lang3.StringUtils#countMatches(String, java.lang.String) by effect.digit()
Add same as [0-9]endCapt()
Shortcut forendCapture()
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 capturesendGr()
Closes current unnamed and unmatching group Shortcut forendCapture()
Use it withgroup()
for prettify code Example: regex().group().maybe("word").count(2).endGr()Mark the expression to end at the last character of the line Same asendOfLine(boolean)
with true argendOfLine
(boolean pEnable) Enable or disable the expression to end at the last character of the lineAdd a string to the expression Syntax sugar forthen(String)
- use it in case: regex().find("string") // when it goes firstgroup()
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);Add universal line break expressionAdd 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://") //truemaybe
(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.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)+nonDigit()
Add non-digit: [^0-9]nonSpace()
Add non-whitespace character: [^\s]Add non-word character: [^\w]Adds an alternative expression to be matched based on an array of valuesAdds "+" 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 2Add a alternative expression to be matched Issue #32Add expression to match a range (or multiply ranges) Usage: .range(from, to [, from, to ...removeModifier
(char pModifier) private String
Escapes any non-word char with two backslashes used by any method, exceptadd(String)
searchOneLine
(boolean pEnable) Add expression that matches something that might appear once (or more)somethingButNot
(String pValue) space()
Add whitespace character, same as [ \t\n\x0B\f\r]Mark the expression to start at the beginning of the line Same asstartOfLine(boolean)
with true argstartOfLine
(boolean pEnable) Enable or disable the expression to start at the beginning of the linetab()
Add expression to match a tab character (' ')Add a string to the expressionTurn ON matching with ignoring case Example: // matches "a" // matches "A" regex().find("a").withAnyCase()withAnyCase
(boolean pEnable) word()
Add word, same as [a-zA-Z_0-9]+Add word boundary: \bwordChar()
Add word character, same as [a-zA-Z_0-9]Adds "*" char to regexp, means zero or more times repeated Same effect asatLeast(int)
with "0" argument
-
Field Details
-
prefixes
-
source
-
suffixes
-
modifiers
private int modifiers -
SYMBOL_MAP
-
-
Constructor Details
-
Builder
Builder()Package private. UseVerbalExpression.regex()
to build a new one- Since:
- 1.2
-
-
Method Details
-
sanitize
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
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
-
add
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
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
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
Mark the expression to start at the beginning of the line Same asstartOfLine(boolean)
with true arg- Returns:
- this builder
-
endOfLine
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
Mark the expression to end at the last character of the line Same asendOfLine(boolean)
with true arg- Returns:
- this builder
-
then
Add a string to the expression- Parameters:
pValue
- - the string to be looked for (sanitized)- Returns:
- this builder
-
find
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
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
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
Add expression that matches anything (includes empty string)- Returns:
- this builder
-
anythingBut
Add expression that matches anything, but not passed argument- Parameters:
pValue
- - the string not to match- Returns:
- this builder
-
something
Add expression that matches something that might appear once (or more)- Returns:
- this builder
-
somethingButNot
-
lineBreak
Add universal line break expression- Returns:
- this builder
-
br
Shortcut forlineBreak()
- Returns:
- this builder
-
tab
Add expression to match a tab character (' ')- Returns:
- this builder
-
word
Add word, same as [a-zA-Z_0-9]+- Returns:
- this builder
-
wordChar
Add word character, same as [a-zA-Z_0-9]- Returns:
- this builder
-
nonWordChar
Add non-word character: [^\w]- Returns:
- this builder
-
nonDigit
Add non-digit: [^0-9]- Returns:
- this builder
-
digit
Add same as [0-9]- Returns:
- this builder
-
space
Add whitespace character, same as [ \t\n\x0B\f\r]- Returns:
- this builder
-
nonSpace
Add non-whitespace character: [^\s]- Returns:
- this 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
-
any
Shortcut toanyOf(String)
- Parameters:
value
- - CharSequence every char from can be matched- Returns:
- this builder
-
range
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
-
removeModifier
-
withAnyCase
-
withAnyCase
Turn ON matching with ignoring case Example: // matches "a" // matches "A" regex().find("a").withAnyCase()- Returns:
- this builder
-
searchOneLine
-
multiple
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
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
Adds "*" char to regexp, means zero or more times repeated Same effect asatLeast(int)
with "0" argument- Returns:
- this builder
- Since:
- 1.2
-
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
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:
-
atLeast
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:
-
or
Add a alternative expression to be matched Issue #32- Parameters:
pValue
- - the string to be looked for- Returns:
- this builder
-
oneOf
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
Adds capture - open brace to current position and closed to suffixes- Returns:
- this builder
-
capture
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
Shortcut forcapture()
- Returns:
- this builder
- Since:
- 1.2
-
capt
Shortcut forcapture(String)
- Returns:
- this builder
- Since:
- 1.6
-
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
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
Shortcut forendCapture()
- Returns:
- this builder
- Since:
- 1.2
-
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
-