Simply Regex
Simply Regex is java API library which provides easy way to build regex.
Prerequisite:
To start using you have to add dependency of simply-regex to your maven/gradle/ivy project. Please refer Releases page for latest releases & dependency information.
Basic Example:
This is the simplest regex example using simply regex.
1 2 3 4 5 6 7 8 |
// Basic Example import com.itsallbinary.simplyregex.SimpleRegex; String builtRegex = SimpleRegex.regex().startingWith().exactString("abc").build(); Pattern pattern = Pattern.compile(builtRegex); boolean isMatch = pattern.matcher(testString).matches(); |
This will build regex as = ^abc
Input: testString = “abc” Output: isMatch = true
Input: testString = “def” Output: isMatch = false
Chaining multiple patterns
You can chain multiple patterns to create entire complex regex as shown in below example.
1 2 3 4 5 6 7 8 9 10 |
// Chaining example import com.itsallbinary.simplyregex.SimpleRegex; String builtRegex = SimpleRegex.regex().startingWith().exactString("abc") .then().oneOfTheCharacters('d', 'e', 'f') .build(); Pattern pattern = Pattern.compile(builtRegex); boolean isMatch = pattern.matcher(testString).matches(); |
This will build regex as = ^abc[def]
Input: testString = “abcd” Output: isMatch = true
Input: testString = “abcg” Output: isMatch = false
Special wildcard character classes
Simply regex API provides easy readable methods for wild card character classes as shown in below example.
1 2 3 4 5 6 7 8 |
// Special wildcard example import com.itsallbinary.simplyregex.SimpleRegex; String builtRegex = SimpleRegex.regex().startingWith().anyDigitChar().build(); Pattern pattern = Pattern.compile(builtRegex); boolean isMatch = pattern.matcher(testString).matches(); |
This will build regex as = ^\d
Input: testString = “1” Output: isMatch = true
Input: testString = “a” Output: isMatch = false
Easy & simple quantifiers
Simply regex API provides easy readable methods for quantifiers as shown below.
1 2 3 4 5 6 7 8 |
// Quantifier example import com.itsallbinary.simplyregex.SimpleRegex; String builtRegex = SimpleRegex.regex().anywhereInText().oneOrMoreOf('a').build(); Pattern pattern = Pattern.compile(builtRegex); boolean isMatch = pattern.matcher(testString).matches(); |
This will build regex as = a+
Input: testString = “” Output: isMatch = false
Input: testString = “a” Output: isMatch = true
Input: testString = “aaa” Output: isMatch = true
Simple way to mix & match groups
Simply regex API provides easy & readable way to create groups & mix with other regex functions.
1 2 3 4 5 6 7 8 9 10 |
// Groups example import com.itsallbinary.simplyregex.SimpleRegex; String builtRegex = SimpleRegex.regex().anywhereInText() .oneOrMoreOf(groupHaving().exactString("abc").then().anyDigitChar().build()) .build() Pattern pattern = Pattern.compile(builtRegex); boolean isMatch = pattern.matcher(testString).matches(); |
This will build regex as = (abc\d)+
Input: testString = “abc1abc2” Output: isMatch = true
Input: testString = “abcz” Output: isMatch = false
Simple capture groups
Simple & fluent way to create capture groups.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// Capture Group Example String builtRegex = SimpleRegex.regex().anywhereInText() .exactString("My Name is ") .then() .group( groupHaving().oneOrMoreOf(charThatIs().anyWordChar().build() ).build()) .then() .exactString(". ") .build(); String testString = "My Name is John. My Name is Merry. My Name is Rock. "; Matcher matcher = Pattern.compile(builtRegex).matcher(testString); matcher.find(); assertEquals("John", matcher.group(1)); matcher.find(); assertEquals("Merry", matcher.group(1)); matcher.find(); assertEquals("Rock", matcher.group(1)); |
Capture groups with name
Capture groups can be given name as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Capture group with name String builtRegex = SimpleRegex.regex().anywhereInText() .exactString("My Name is ").then() .group( groupWithName("personname").having().oneOrMoreOf(charThatIs().anyWordChar().build() ).build()) .then() .exactString(". ").build(); String testString = "My Name is John. My Name is Merry. My Name is Rock. "; Matcher matcher = Pattern.compile(builtRegex).matcher(testString); matcher.find(); assertEquals("John", matcher.group("personname")); matcher.find(); assertEquals("Merry", matcher.group("personname")); matcher.find(); assertEquals("Rock", matcher.group("personname")); |