简体   繁体   English

使用正则表达式提取捕获组

[英]Extracting Capture Group Using Regex

I am simply trying to capture a string surrounded static text. 我只是想捕获一个包含在静态文本中的字符串。 I'll illustrate by example. 我将举例说明。 Here is the string I'm working with ... 这是我正在使用的字符串...

String idInfo = "Any text up here\n" +
                "Here is the id\n" +
                "\n" +
                "?a0 12 b5\n" +
                "&Edit Properties...\n" +
                "And any text down here";

Or in pretty print ... 或以精美的印刷品...

Any text up here
Here is the id

?a0 12 b5
&Edit Properties...
And any text down here

And I'm using the following code to try and print the ID number ... 我正在使用以下代码尝试打印ID号...

Pattern p = Pattern.compile("Here is the id\n\n\?[a-z0-9]{2}( [a-z0-9]{2}){2}\n&Edit Properties...);
Matcher m = p.matcher(idInfo);
String idNum = m.group(1);
System.out.println(idNum);

And I want to simply output the ID number, so the output I desire for this example is ... 而且我只想输出ID号,所以我希望在此示例中输出的是...

a0 12 b5

However, I'm getting a "No match found" exception when I run my code. 但是,运行代码时出现“找不到匹配项”异常。 What am I doing wrong? 我究竟做错了什么? Is there a simpler, more elegant way to accomplish my solution? 有没有更简单,更优雅的方法来完成我的解决方案?

You need to let Matcher find match before you use it. 您需要先让Matcher find匹配项,然后再使用它。 So invoke m.find() (or m.matches() depending on your goal) before accessing m.group(1); 因此,在访问m.group(1);之前,先调用m.find() (或m.matches()取决于您的目标m.group(1); . Also check if match was actually found (if m.find() retuned true ) to make sure that group 1 exists. 还要检查是否实际找到匹配项(如果m.find()重新设置为true ),以确保组1存在。

Other thing is that string representing your regex is incorrect. 另一件事是代表您的正则表达式的字符串不正确。 If you want to escape ? 你想逃跑? in regex you need to write \\ as two "\\\\" because \\ is special character in String (used for instance to create \\n ) which also needs escaping. 在正则表达式中,您需要将\\写为两个"\\\\"因为\\是String中的特殊字符(例如,用于创建\\n ),这也需要转义。

Last thing you pointed yourself in comment is that ( [a-z0-9]{2}) wouldn't put match a0 12 b5 in group 1. To solve this problem we need to surround [a-z0-9]{2}( [a-z0-9]{2}){2} in parenthesis. 您在注释中指出的最后一件事是( [a-z0-9]{2})不会在组1中放置匹配a0 12 b5 。要解决此问题,我们需要将[a-z0-9]{2}( [a-z0-9]{2}){2}括起来。

So try maybe 所以也许

Pattern p = Pattern.compile("Here is the id\n\n\\?([a-z0-9]{2}( [a-z0-9]{2}){2})\n&Edit Properties...");
Matcher m = p.matcher(idInfo);

if (m.find()) {//or while(m.find())
    String idNum = m.group(1);
    System.out.println(idNum);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM