幫助構建復雜正規表達式的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
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!