3

我正在尝试在 Java 中创建一个正则表达式来匹配特定单词的模式以查找具有相同模式的其他单词。例如,单词“tooth”具有模式 12213,因为 't' 和 'o' 都重复。我希望正则表达式匹配“牙齿”等其他词。

所以这是我使用反向引用的尝试。在此特定示例中,如果第二个字母与第一个字母相同,则它应该失败。此外,最后一个字母应该与其他字母不同。

String regex = "([a-z])([a-z&&[^\1]])\\2\\1([a-z&&[^\1\2]])";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher("tooth");

//This works as expected
assertTrue(m.matches());

m.reset("tooto");
//This should return false, but instead returns true
assertFalse(m.matches());

如果我删除最后一组,我已经验证它适用于像“嘟嘟”这样的例子,即以下,所以我知道反向引用正在工作到这一点:

String regex = ([a-z])([a-z&&[^\1]])\\2\\1";

但是,如果我将最后一组添加回模式的末尾,就好像它不再识别方括号内的反向引用。

我做错了什么,还是这是一个错误?

4

2 回答 2

4

试试这个:

(?i)\b(([a-z])(?!\2)([a-z])\3\2(?!\3)[a-z]+)\b

解释

(?i)           # Match the remainder of the regex with the options: case insensitive (i)
\b             # Assert position at a word boundary
(              # Match the regular expression below and capture its match into backreference number 1
   (              # Match the regular expression below and capture its match into backreference number 2
      [a-z]          # Match a single character in the range between “a” and “z”
   )
   (?!            # Assert that it is impossible to match the regex below starting at this position (negative lookahead)
      \2             # Match the same text as most recently matched by capturing group number 2
   )
   (              # Match the regular expression below and capture its match into backreference number 3
      [a-z]          # Match a single character in the range between “a” and “z”
   )
   \3             # Match the same text as most recently matched by capturing group number 3
   \2             # Match the same text as most recently matched by capturing group number 2
   (?!            # Assert that it is impossible to match the regex below starting at this position (negative lookahead)
      \3             # Match the same text as most recently matched by capturing group number 3
   )
   [a-z]          # Match a single character in the range between “a” and “z”
      +              # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
\b             # Assert position at a word boundary

代码

try {
    Pattern regex = Pattern.compile("(?i)\\b(([a-z])(?!\\2)([a-z])\\3\\2(?!\\3)[a-z]+)\\b");
    Matcher regexMatcher = regex.matcher(subjectString);
    while (regexMatcher.find()) {
        for (int i = 1; i <= regexMatcher.groupCount(); i++) {
            // matched text: regexMatcher.group(i)
            // match start: regexMatcher.start(i)
            // match end: regexMatcher.end(i)
        }
    } 
} catch (PatternSyntaxException ex) {
    // Syntax error in the regular expression
}

看到它在这里播放。希望这可以帮助。

于 2012-07-19T05:13:46.740 回答
4

如果你打印你的正则表达式,你就会知道哪里出了问题,你的组中的反向引用实际上被 Java 转义以产生一些奇怪的字符。因此它不能按预期工作。例如:

m.reset("oooto");
System.out.println(m.matches());

也打印

真的

此外,&&在正则表达式中不起作用,您必须改用前瞻。此表达式适用于您上面的示例:

String regex = "([a-z])(?!\\1)([a-z])\\2\\1(?!(\\1|\\2))[a-z]";

表达式(?!\\1)向前看以查看下一个字符不是表达式中的第一个字符,而无需向前移动正则表达式光标。

于 2012-07-19T05:15:22.113 回答