Java正则表达式没有多次出现字符串

时间:2014-09-07 07:24:39

标签: java regex string multiline

我知道这里回答的问题各不相同

我试图通过解决方案并为我的需求提出正则表达式。我有一行多行文本,既没有固定的起始位置也没有特定行的结束位置。

<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(&quot;link&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,而不是其他任何内容。

更新 - 所需的输出

 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;)

3 个答案:

答案 0 :(得分:1)

你总是把小组(1)作为问题。使用

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

答案 1 :(得分: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

答案 2 :(得分:0)

您的模式存在问题。试试这个:

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;);)