帮助构建复杂正规表达式的Java类库:JavaVerbalExpressions

分享到:

VerbalExpressions是一个Java类库,能够帮助构建复杂正规表达式。

VerbalExpression testRegex = VerbalExpression.regex()
.startOfLine().then("http").maybe("s")
.then("://")
.maybe("www.").anythingButNot(" ")
.endOfLine()
.build();
// Create an example URL
String url = "https://www.google.com";
// Use VerbalExpression's testExact() method to test if the entire string matches the regex
testRegex.testExact(url); //True
testRegex.toString(); // Outputs the regex used:
// ^(?:http)(?:s)?(?:\:\/\/)(?:www\.)?(?:[^\ ]*)$
VerbalExpression testRegex = VerbalExpression.regex()
.startOfLine().then("abc").or("def")
.build();
String testString = "defzzz";
//Use VerbalExpression's test() method to test if parts if the string match the regex
testRegex.test(testString);
// true
testRegex.testExact(testString);  // false
testRegex.getText(testString);
// returns: def

项目主页:http://www.open-open.com/lib/view/home/1416744770039

昵    称:
验证码:

相关文档: