gpt4 book ai didi

java - 正则表达式模式可选组取决于空间

转载 作者:行者123 更新时间:2023-12-02 11:24:54 25 4
gpt4 key购买 nike

我对正则表达式非常陌生,我需要创建一个可用于匹配不同文本值(案例)的模式。我可以使用创建的模式,但它只能在单一情况下使用。我想最大化搜索模式,以便它可以用于不同的搜索文本。

顺便说一下,我使用的是 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 demo

这是regex demo :

(\w+)([:!])(\p{Punct}?)(.*?)(\p{Punct}?)(?=$|,)

详细信息

  • (\w+) - 第 1 组:一个或多个单词字符
  • ([:!]) - 第 2 组::!
  • (\p{Punct}?) - 第 3 组:可选标点符号
  • (.*?) - 第 4 组:除换行符之外的任何 0+ 个字符
  • (\p{Punct}?) - 第 5 组:可选标点符号
  • (?=$|,) - 字符串结尾或 , 应立即出现在当前位置的右侧(但它们不会添加到匹配值,因为它是正向前瞻)。

关于java - 正则表达式模式可选组取决于空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49688362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com