【问题标题】:Java RegExpr with Pattern class multigroup带有模式类多组的 Java RegEx
【发布时间】:2014-12-17 09:38:32
【问题描述】:

我有这种情况:

我的字符串:

Try a lot of match groups

[COD_BODY="AZI_DIP-ANAG_AZI", LABEL="Apri link1"] come on Taranto

[ COD_BODY="AZI_DIP-ANAG_AZI", LABEL="Apri link2"]

[ COD_BODY ="AZI_DIP-ANAG_AZI", LABEL="Apri link3"]

[ COD_BODY = "AZI_DIP-ANAG_AZI", LABEL="Apri link4"]

[ COD_BODY = "AZI_DIP-ANAG_AZI", LABEL ="Apri link5"]

[ COD_BODY = "AZI_DIP-ANAG_AZI", LABEL = "Apri link6"]

[ COD_BODY = "AZI_DIP-ANAG_AZI" , LABEL = "Apri link5" ]

我已经写了这个掩码(用 Java):

"(\\[(\\s*)\\bCOD_BODY(\\s*)=(\\s*)\"(.*)?\"(\\s*),(\\s*)LABEL(\\s*)=(\\s*)\"(.*)?\"(\\s*)\\]*)"

我在 Pattern.compile 中使用过这个模式

现在...如果我在这些网站上尝试:

http://www.regexr.com/

http://ocpsoft.org/tutorials/regular-expressions/java-visual-regex-tester

使用这种模式:

(\[(\s*)\bCOD_BODY(\s*)=(\s*)"(.*)?"(\s*),(\s*)LABEL(\s*)=(\s*)"(.*)?"(\s*)\]*)

我已经删除了在 Java 中用于转义特殊字符的双 \

结果是:找到 7 个匹配项!

但在 Java 中 while(matcher.find()) 只循环一次而不是七次!

请问,我的错在哪里?

如果您有更多信息,请评论此问题。

【问题讨论】:

    标签: java regex


    【解决方案1】:

    我无法重现您的问题:

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    
    public class Regex {
    
        public Regex() {
            // TODO Auto-generated constructor stub
        }
    
        public static void main(String[] args) {
            String input = "Try a lot of match groups\r\n" + 
                    "\r\n" + 
                    "[COD_BODY=\"AZI_DIP-ANAG_AZI\", LABEL=\"Apri link1\"] come on Taranto\r\n" + 
                    "\r\n" + 
                    "[ COD_BODY=\"AZI_DIP-ANAG_AZI\", LABEL=\"Apri link2\"]\r\n" + 
                    "\r\n" + 
                    "[ COD_BODY =\"AZI_DIP-ANAG_AZI\", LABEL=\"Apri link3\"]\r\n" + 
                    "\r\n" + 
                    "[ COD_BODY = \"AZI_DIP-ANAG_AZI\", LABEL=\"Apri link4\"]\r\n" + 
                    "\r\n" + 
                    "[ COD_BODY = \"AZI_DIP-ANAG_AZI\", LABEL =\"Apri link5\"]\r\n" + 
                    "\r\n" + 
                    "[ COD_BODY = \"AZI_DIP-ANAG_AZI\", LABEL = \"Apri link6\"]\r\n" + 
                    "\r\n" + 
                    "[ COD_BODY = \"AZI_DIP-ANAG_AZI\" , LABEL = \"Apri link5\" ]";
            Pattern pattern = Pattern.compile("(\\[(\\s*)\\bCOD_BODY(\\s*)=(\\s*)\"(.*)?\"(\\s*),(\\s*)LABEL(\\s*)=(\\s*)\"(.*)?\"(\\s*)\\]*)");
            Matcher matcher = pattern.matcher(input);
            while(matcher.find()) {
                System.out.println(matcher.group());
            }
        }
    
    }
    

    结果:

    [COD_BODY="AZI_DIP-ANAG_AZI", LABEL="Apri link1"]
    [ COD_BODY="AZI_DIP-ANAG_AZI", LABEL="Apri link2"]
    [ COD_BODY ="AZI_DIP-ANAG_AZI", LABEL="Apri link3"]
    [ COD_BODY = "AZI_DIP-ANAG_AZI", LABEL="Apri link4"]
    [ COD_BODY = "AZI_DIP-ANAG_AZI", LABEL ="Apri link5"]
    [ COD_BODY = "AZI_DIP-ANAG_AZI", LABEL = "Apri link6"]
    [ COD_BODY = "AZI_DIP-ANAG_AZI" , LABEL = "Apri link5" ]
    

    【讨论】:

    • 感谢您的启发。错误是我在
      HTML 标记中的预转换 \n\r。于是我交换了两个操作,让Java识别7组!
    【解决方案2】:

    如果你的模式匹配整个字符串,你不应该在循环中调用find()。相反,您必须从匹配器中提取组:

    Matcher m = pattern.matcher(str);}
    if (!m.find()) { // call find only once!
       return;
    }
    int n = m.groupCount();
    for(int i = 1; i <= n; i++) { // starting from 1
        String capture = m.group(i); // this is the captured group.
        // deal with capture
    }
    

    【讨论】:

    • 我认为该模式不会尝试匹配整个字符串。 (没有$^。)
    • @GáborBakos,我的意思是它完全匹配包含所有需要的捕获组的片段。
    • 了解downvoter 投下完全正确答案的原因非常有趣。
    • 我认为您正在尝试回答其他问题。 (虽然我可能是错的。)这个问题似乎是关于 find() 方法,而不是组。 find() 方法当然可以多次调用,每次调用后都会前进到下一次。 OP 表示,使用其他工具,他或她能够找到 7 个具有相似模式的事件,而使用 Java 只有 1 个。
    • 这个问题是关于模式API的错误使用。 OP 不应在循环中调用 find() 来获取他所有捕获的组。这是我推荐的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-06
    • 2019-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    相关资源
    最近更新 更多