正则表达式,用于匹配具有特定模式的数字

时间:2019-05-31 20:01:42

标签: java regex regex-lookarounds regex-group regex-greedy

我正在尝试在html中匹配多个组,但我停留在只匹配第一个组而离开其他组的位置。

示例字符串:

<div style="left: 0px; top: 0px; margin-left: 36px; position: 1234 1234 1234 1234 relative;">8765 5645 5434 5444 <a class="x6i" id="linkedTable:3:hgi" onclick="_uixt_linkedTable.action('hide','3',this);return false;" href="#"><img width="16" height="16" title="Select to collapse" alt="Select to collapse" src="/Advisor/baAdvisorWeb/images/treeMinus.png" border="0"></a>5466 1604 2694 7493 </div>

正则表达式使用:<\s*div\s[^>]*[^0-9]?(\d{4}\s?\d{4}\s?\d{4}\s?\d{4})[^0-9].*>

如何匹配xxxx xxxx xxxx xxxx的出现?

2 个答案:

答案 0 :(得分:1)

如果我们要捕获textContent中的重复四位数,则可以将表达式简化为:

>(\s+)?(\d{4}\s?\d{4}\s?\d{4}\s?\d{4})(\s+)?<

,它可能会起作用。

Demo 1

我们还可以缩小边界:

>(([0-9]{4}\s){4})<

Demo 2

或:

>(([0-9\s]+))<

,如果它可能仍然起作用。

Demo 3

测试

import java.util.regex.Matcher;
import java.util.regex.Pattern;

final String regex = ">(\\s+)?(\\d{4}\\s?\\d{4}\\s?\\d{4}\\s?\\d{4})(\\s+)?<";
final String string = "<div style=\"left: 0px; top: 0px; margin-left: 36px; position: 1234 1234 1234 1234 relative;\">8765 5645 5434 5444 <a class=\"x6i\" id=\"linkedTable:3:hgi\" onclick=\"_uixt_linkedTable.action('hide','3',this);return false;\" href=\"#\"><img width=\"16\" height=\"16\" title=\"Select to collapse\" alt=\"Select to collapse\" src=\"/Advisor/baAdvisorWeb/images/treeMinus.png\" border=\"0\"></a>5466 1604 2694 7493 </div>\n";

final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);

while (matcher.find()) {
    System.out.println("Full match: " + matcher.group(0));
    for (int i = 1; i <= matcher.groupCount(); i++) {
        System.out.println("Group " + i + ": " + matcher.group(i));
    }
}

RegEx电路

jex.im可视化正则表达式:

enter image description here

答案 1 :(得分:0)

正则表达式使用:<div(?:.)*((\d{4}\s?\d{4}\s?\d{4}\s?\d{4}))(?:.)*<\/div>

import java.util.regex.Matcher;
import java.util.regex.Pattern;

final String regex = "<div(?:.)*((\\d{4}\\s?\\d{4}\\s?\\d{4}\\s?\\d{4}))(?:.)*<\\/div>";
final String string = "<div style=\"left: 0px; top: 0px; margin-left: 36px; position: 1234 1234 1234 1234 relative;\">8765 5645 5434 5444 <a class=\"x6i\" id=\"linkedTable:3:hgi\" \\d{4}\\s?\\d{4}\\s?\\d{4}\\s?\\d{4}onclick=\"_uixt_linkedTable.action('hide','3',this);return false;\" href=\"#\"><img width=\"16\" height=\"16\" title=\"Select to collapse\" alt=\"Select to collapse\" src=\"/Advisor/baAdvisorWeb/images/treeMinus.png\" border=\"0\"></a>5466 1604 2694 7493 </div>\n\n";

final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);

while (matcher.find()) {
    System.out.println("Full match: " + matcher.group(0));
    for (int i = 1; i <= matcher.groupCount(); i++) {
        System.out.println("Group " + i + ": " + matcher.group(i));
    }
}

输出:

Group 1: 5466 1604 2694 7493
Group 2: 5466 1604 2694 7493