【问题标题】:Java regex not picking up multiple occurences of stringJava 正则表达式没有拾取多次出现的字符串
【发布时间】:2014-09-07 07:24:39
【问题描述】:

我知道这里回答了各种各样的问题

我已经尝试过解决方案,并根据我的需要提出一个正则表达式。我有一个多行的文本字符串,既没有固定的起始位置,也没有特定行的结束位置。

<a name='bill_pay' href='javascript:goto(&#39;billpay&#39;);' class='fsdnav-top-menu-item'>Bill Pay <span class='fsdnav-ada-hidden'>link and menu. Press enter to navigate to this link. Press control + space to open submenu.

To move through submenu items press tab and then press up or down arrow.</span> </a>
<a name='bill_pay' href='javascript:goto(&#39;findmyinfo&#39;);' class='fsdnav-top-menu-item'>
Bill Pay <span class='fsdnav-ada-hidden'>link and menu. Press enter to navigate to this link. Press control + space to open submenu.

To move through submenu items press tab and then press up or down arrow.</span> </a>
<a name='bill_pay' href='#' onClick='OOLPopUp(&#39;/myaccounts/brain/redirect.go?target=findmyroutingnumber&#39;,&#39;ool&#39;,&#39;currentPage&#39;);return false;' class='fsdnav-top-menu-item'>
Bill Pay <span class='fsdnav-ada-hidden'>link and menu. Press enter to navigate to this link. Press control + space to open submenu.
To move through submenu items press tab and then press up or down arrow.</span> </a>

我想从javascript:goto(&amp;quot;link&amp;quot;) 中提取以下内容(链接值代表什么) 上面的正则表达式中有多个这样的事件,但我使用的正则表达式只返回一个事件。我想全部归还。我的代码块如下所示

private static final Pattern PATTERN_WITH_ASCII_QUOTES =
    Pattern.compile("^.*goto\\(&#39;(\\w+)&#39;\\).*",
        Pattern.MULTILINE|Pattern.DOTALL);

// "str" is the string representation of the text above.
Matcher m = PATTERN_WITH_ASCII_QUOTES.matcher(str);
while (m.find()) {
    System.out.println(m.group(1));
}

结果输出始终为findmyinfo,仅此而已。

UPDATE - 所需的输出是

 billpay (from javascript:goto(&#39;billpay&#39;);)
 findmyinfo (from javascript:goto(&#39;findmyinfo&#39;);)

我也想提取

/myaccounts/brain/redirect.go?target=findmyroutingnumber&#39;,&#39;ool&#39;,&#39;currentPage from OOLPopUp(&#39;/myaccounts/brain/redirect.go?target=findmyroutingnumber&#39;,&#39;ool&#39;,&#39;currentPage&#39;)

【问题讨论】:

  • 你的预期输出是什么?

标签: java regex string multiline


【解决方案1】:

您需要将OLLPopUpgoto 添加到非捕获组中,以获取第一个、第二个和第三个值。

 ^.*?(?:goto|OOLPopUp)\(&#39;(.*?)&#39;\).*

DEMO

String s = "<a name='bill_pay' href='javascript:goto(&#39;billpay&#39;);' class='fsdnav-top-menu-item'>Bill Pay <span class='fsdnav-ada-hidden'>link and menu. Press enter to navigate to this link. Press control + space to open submenu.\n" + 
        "To move through submenu items press tab and then press up or down arrow.</span> </a>\n" +
        "<a name='bill_pay' href='javascript:goto(&#39;findmyinfo&#39;);' class='fsdnav-top-menu-item'>\n" +
        "<a name='bill_pay' href='#' onClick='OOLPopUp(&#39;/myaccounts/brain/redirect.go?target=findmyroutingnumber&#39;,&#39;ool&#39;,&#39;currentPage&#39;);return false;' class='fsdnav-top-menu-item'>\n" +
        "Bill Pay <span class='fsdnav-ada-hidden'>link and menu. Press enter to navigate to this link. Press control + space to open submenu.";
Pattern regex = Pattern.compile("^.*?(?:goto|OOLPopUp)\\(&#39;(.*?)&#39;\\).*", Pattern.MULTILINE);
 Matcher matcher = regex.matcher(s);
 while(matcher.find()){
        System.out.println(matcher.group(1));
}

输出:

billpay
findmyinfo
/myaccounts/brain/redirect.go?target=findmyroutingnumber&#39;,&#39;ool&#39;,&#39;currentPage

String s = "<a name='bill_pay' href='javascript:goto(&#39;billpay&#39;);' class='fsdnav-top-menu-item'>Bill Pay <span class='fsdnav-ada-hidden'>link and menu. Press enter to navigate to this link. Press control + space to open submenu.\n" + 
        "To move through submenu items press tab and then press up or down arrow.</span> </a>\n" +
        "<a name='bill_pay' href='javascript:goto(&#39;findmyinfo&#39;);' class='fsdnav-top-menu-item'>\n" +
        "<a name='bill_pay' href='#' onClick='OOLPopUp(&#39;/myaccounts/brain/redirect.go?target=findmyroutingnumber&#39;,&#39;ool&#39;,&#39;currentPage&#39;);return false;' class='fsdnav-top-menu-item'>\n" +
        "Bill Pay <span class='fsdnav-ada-hidden'>link and menu. Press enter to navigate to this link. Press control + space to open submenu.";
Pattern regex = Pattern.compile("^(?:.*?goto\\(&#39;(\\w+)&#39;\\).*|.*?OOLPopUp\\(&#39;(.+?&#39;\\)).*)$", Pattern.MULTILINE);
 Matcher matcher = regex.matcher(s);
 while(matcher.find()){
        System.out.println(matcher.group(1) != null ?
                matcher.group(1) : matcher.group(2)
                );
}

输出:

billpay
findmyinfo
/myaccounts/brain/redirect.go?target=findmyroutingnumber&#39;,&#39;ool&#39;,&#39;currentPage&#39;)

IDEONE

【讨论】:

  • 我有另一个澄清,我希望你不介意。我有另一组网址,例如 Bill Pay,替换 '和 \'。我试图对您的正则表达式进行逆向工程,但似乎没有任何效果。对于我尝试的每个变体,我都会得到 IndexOutOfBoundsException。我该如何添加它?
  • 是的。这正是我想要的效果。虽然,我们在 HTML 文档中使用单引号,所以我们有一个类似 Bill Pay 的设置。我也希望能够从这里提取价值。
  • 我不能把它放在正则表达式中。 Pattern.compile("^.*?(?:goto|OOLPopUp)\('|'(.*?)'|'\).*", Pattern.MULTILINE);返回一个 ArrayOutOfBoundsException。
  • 用双反斜杠替换单反斜杠
  • 在哪里用双斜杠替换单反斜杠? Pattern.compile("^.*?(?:goto|OOLPopUp)\('|\'(.*?)'|\'\).*", Pattern.MULTILINE);抛出 ArrayOutOfBoundsException
【解决方案2】:

您总是选择作为探针的组 (1)。使用

while (m.find()) {
    System.out.println(m.group());
}

【讨论】:

  • 不打印文本。第一个条目是整个字符串,然后什么都没有。我没有得到提取的字符串。
【解决方案3】:

您的模式有问题。试试这个:

Pattern.compile("goto\\(&#39;(\\w+)&#39;\\)",
                    Pattern.MULTILINE|Pattern.DOTALL);

在打印结果的时候也可以试试:

System.out.println(m.group(1) + " ( from " + str.substring(m.toMatchResult().start(), m.toMatchResult().end()) + " )");

输出是这样的:

billpay (from goto(&#39;billpay&#39;);)
findmyinfo (from goto(&#39;findmyinfo&#39;);)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    • 2019-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多