我对正则表达式很陌生,我需要创建一个可用于匹配不同文本值(案例(的模式。我可以使用创建的模式,但它只能在一种情况下使用。我想最大化搜索模式,以便它可以用于不同的搜索文本。
顺便说一下,我正在使用Java 8。
目的:
按组显示 matcher.find((。
示例搜索文本和预期输出(组(:
Search Text: "employeeName:*borgy*";
Expected Output:
-
(employeeName) (:) (*) (borgy) (*)
-
Search Text: "employeeName:Borgy Manotoy*";
Expected Output:
-
(employeeName) (:) () (Borgy Manotoy) (*)
-
Search Text: "employeeName:*Borgy Manotoy*";
Expected Output:
-
(employeeName) (:) (*) (Borgy Manotoy) (*)
-
Search Text: "employeeEmail:*borgymanotoy@iyotbihagay.com*";
Expected Output:
-
(employeeEmail) (:) (*) (borgymanotoy@iyotbihagay.com) (*)
-
Search Text: "employeeEmail:borgymanotoy@iyotbihagay.com";
Expected Output:
-
(employeeEmail) (:) () (borgymanotoy@iyotbihagay.com) ()
-
Search Text: "employeeName:*Manotoy*, employeeEmail:*@iyotbihagay.*";
Expected Output:
-
(employeeName) (:) (*) (Manotoy) (*)
(employeeEmail) (:) (*) (@iyotbihagay.com) (*)
-
Search Text: "employeeName:*Manotoy*, employeeEmail:*@iyotbihagay.*, employeeRole:*bouncer*";
Expected Output:
-
(employeeName) (:) (*) (Manotoy) (*)
(employeeEmail) (:) (*) (@iyotbihagay.com) (*)
(employeeRole) (:) (*) (bouncer) (*)
-
搜索模式:
String searchPattern = "(\w+?)(:|!)(\p{Punct}?)(\w+?) (.+?)?(\p{Punct}?),";
示例搜索文本:
String text1 = "employeeName:borgy";
String text2 = "employeeName:Borgy*";
String text3 = "employeeName:*borgy*";
String text4 = "employeeName:*Borgy*";
String text5 = "employeeName:*Borgy Manotoy*";
String text6 = "employeeEmail:*borgymanotoy@iyotbihagay.com*";
String text7 = "employeeEmail:borgymanotoy@iyotbihagay.com";
String text8 = "employeeEmail:borgymanotoy@iyotbihagay.*";
String text9 = "employeeEmail:*@iyotbihagay.*";
String text10 = "employeeName:*Manotoy*, employeeEmail:*@iyotbihagay.*";
使用给定模式搜索文本:
processUserSearch(text1, searchPattern);
processUserSearch(text2, searchPattern);
processUserSearch(text3, searchPattern);
...
processUserSearch(text10, searchPattern);
找到显示
private void processUserSearch(String searchText, String searchPattern) {
if (!Util.isEmptyOrNull(searchText) && !Util.isEmptyOrNull(searchPattern)) {
Pattern pattern = Pattern.compile(searchPattern);
Matcher matcher = pattern.matcher(searchText + ",");
while(matcher.find()) {
System.out.println("[matcher-count]: " + matcher.groupCount());
System.out.print("found: ");
for (int x = 1; x <= matcher.groupCount(); x++) {
System.out.print("(" + matcher.group(x) + ") ");
}
System.out.println("n");
}
}
}
我建议使用
private static final Pattern pattern = Pattern.compile("(\w+)([:!])(\p{Punct}?)(.*?)(\p{Punct}?)(?=$|,)");
private static void processUserSearch(String searchText) {
if (!searchText.isEmpty() && searchText != null) {
//if (!Util.isEmptyOrNull(searchText) && !Util.isEmptyOrNull(searchPattern)) {
Matcher matcher = pattern.matcher(searchText);
while(matcher.find()) {
System.out.println(searchText + "n[matcher-count]: " + matcher.groupCount());
System.out.print("found: ");
for (int x = 1; x <= matcher.groupCount(); x++) {
System.out.print("(" + matcher.group(x) + ") ");
}
System.out.println("n");
}
}
}
请注意,您可以在匹配方法之外编译一次,以提高效率。
用作
String[] texts = new String[] { "employeeName:*borgy*","employeeName:Borgy Manotoy*","employeeName:*Borgy Manotoy*",
"employeeEmail:*borgymanotoy@iyotbihagay.com*","employeeEmail:borgymanotoy@iyotbihagay.com",
"employeeName:*Manotoy*, employeeEmail:*@iyotbihagay.*",
"employeeName:*Manotoy*, employeeEmail:*@iyotbihagay.*, employeeRole:*bouncer*"};
for (String s: texts) {
processUserSearch(s);
}
}
查看 Java 演示
这是正则表达式演示:
(w+)([:!])(p{Punct}?)(.*?)(p{Punct}?)(?=$|,)
详
-
(w+)
- 第 1 组:一个或多个单词字符 -
([:!])
- 第2组::
或!
-
(p{Punct}?)
- 第 3 组:可选的标点符号
字符 -
(.*?)
- 第 4 组:除换行符字符以外的任何 0+ 字符 -
(p{Punct}?)
- 第 5 组:可选的标点符号
字符 -
(?=$|,)
- 字符串或,
的末尾应紧邻当前位置的右侧(但它们不会添加到匹配值中,因为它是积极的展望(。
我想最大化搜索模式,以便它可以用于不同的搜索文本。
什么是"不同的搜索文本"?要具体!
你的问题似乎不是Java特有的。您当前的模式包含 (:|!)
,但没有一个示例表明输入中!
如何发生。您可以使用p{Punct}
来匹配名称和电子邮件周围的*
,但除了 *
之外,您没有其他附件的示例。你没有说*
的目的是什么;它们是外壳,通配符模式,什么?
以下模式似乎适用于某些目的:
(?:employee(Name|Email)):([w*@. ]+)