我知道这里回答的问题各不相同
我试图通过解决方案并为我的需求提出正则表达式。我有一行多行文本,既没有固定的起始位置也没有特定行的结束位置。
<a name='bill_pay' href='javascript:goto('billpay');' 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('findmyinfo');' 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('/myaccounts/brain/redirect.go?target=findmyroutingnumber','ool','currentPage');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("link")
中提取以下内容(链接值代表什么)上面的正则表达式中有多个这样的事件,但我使用的正则表达式只返回一次。我想归还所有这些。我的代码块在下面给出
private static final Pattern PATTERN_WITH_ASCII_QUOTES =
Pattern.compile("^.*goto\\('(\\w+)'\\).*",
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('billpay');)
findmyinfo (from javascript:goto('findmyinfo');)
我还想提取
/myaccounts/brain/redirect.go?target=findmyroutingnumber','ool','currentPage from OOLPopUp('/myaccounts/brain/redirect.go?target=findmyroutingnumber','ool','currentPage')
答案 0 :(得分:1)
你总是把小组(1)作为问题。使用
while (m.find()) {
System.out.println(m.group());
}
答案 1 :(得分:1)
您需要按顺序将OLLPopUp
和goto
添加到非捕获组中,以获取第一个,第二个和第三个值。
^.*?(?:goto|OOLPopUp)\('(.*?)'\).*
String s = "<a name='bill_pay' href='javascript:goto('billpay');' 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('findmyinfo');' class='fsdnav-top-menu-item'>\n" +
"<a name='bill_pay' href='#' onClick='OOLPopUp('/myaccounts/brain/redirect.go?target=findmyroutingnumber','ool','currentPage');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)\\('(.*?)'\\).*", Pattern.MULTILINE);
Matcher matcher = regex.matcher(s);
while(matcher.find()){
System.out.println(matcher.group(1));
}
<强>输出:强>
billpay
findmyinfo
/myaccounts/brain/redirect.go?target=findmyroutingnumber','ool','currentPage
或强>
String s = "<a name='bill_pay' href='javascript:goto('billpay');' 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('findmyinfo');' class='fsdnav-top-menu-item'>\n" +
"<a name='bill_pay' href='#' onClick='OOLPopUp('/myaccounts/brain/redirect.go?target=findmyroutingnumber','ool','currentPage');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\\('(\\w+)'\\).*|.*?OOLPopUp\\('(.+?'\\)).*)$", 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','ool','currentPage')
答案 2 :(得分:0)
您的模式存在问题。试试这个:
Pattern.compile("goto\\('(\\w+)'\\)",
Pattern.MULTILINE|Pattern.DOTALL);
同样在打印结果时,您可以尝试:
System.out.println(m.group(1) + " ( from " + str.substring(m.toMatchResult().start(), m.toMatchResult().end()) + " )");
输出如下:
billpay (from goto('billpay');)
findmyinfo (from goto('findmyinfo');)