【问题标题】:Regex find() not true; detect duplicate characters in string正则表达式 find() 不正确;检测字符串中的重复字符
【发布时间】:2016-09-06 00:15:48
【问题描述】:

我正在尝试使用正则表达式检测字符串中是否有任何重复字符。当我在在线正则表达式测试器中测试模式并输入时,它说 find() 应该是真的。但它在我的程序中不起作用。 我使用的信息来自:regex to match a word with unique (non-repeating) characters

发生了什么事?我在 Java 中正确使用了正则表达式吗?

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

public class Test {
    public static void main(String[] args) {
        Pattern pat = Pattern.compile("(.).*\1");
        String s = "1112";
        Matcher m = pat.matcher(s);
        if (m.find()) System.out.println("Matches");
        else System.out.println("No Match");
     }
}

【问题讨论】:

    标签: java regex


    【解决方案1】:

    反向引用需要转义

    Pattern pattern = Pattern.compile("(.).*\\1");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-15
      • 1970-01-01
      • 2010-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多