Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
匹配重复关键字的Java正则表达式_Java_Regex - Fatal编程技术网

匹配重复关键字的Java正则表达式

匹配重复关键字的Java正则表达式,java,regex,Java,Regex,如果标题是同一姓氏,即Smith Vs Smith或John Vs John等,我需要筛选文档。。 我正在将整个文档转换为字符串,并根据正则表达式验证该字符串。 有谁能帮我为上述情况编写一个正则表达式。 示例:\w+Vs\1如果a完全理解了您的问题:您有一个像这样的字符串X Vs Y,其中X和Y是两个名称,您想知道X==Y 在这种情况下,一个简单的\w+regex可以做到这一点: String input = "Smith Vs Smith"; // Build the Reg

如果标题是同一姓氏,即Smith Vs Smith或John Vs John等,我需要筛选文档。。 我正在将整个文档转换为字符串,并根据正则表达式验证该字符串。 有谁能帮我为上述情况编写一个正则表达式。


示例:\w+Vs\1

如果a完全理解了您的问题:您有一个像这样的字符串X Vs Y,其中X和Y是两个名称,您想知道X==Y

在这种情况下,一个简单的\w+regex可以做到这一点:

    String input = "Smith Vs Smith";

    // Build the Regex 
    Pattern p = Pattern.compile("(\\w+)");
    Matcher m = p.matcher(input);

    // Store the matches in a list
    List<String> str = new ArrayList<String>();
    while (m.find()) {
        if (!m.group().equals("Vs"))
        {
            str.add(m.group());
        }
    }

    // Test the matches
    if (str.size()>1 && str.get(0).equals(str.get(1)))
        System.out.println(" The Same ");
    else System.out.println(" Not the Same ");
这意味着:一个由1个或多个字符组成的单词,签名为组1,后跟任意字符,后跟组1中的任意字符

更多说明:将regex的一部分置于括号内并引用表达式\1中定义的组在此处实现了这一点

例如:

String s = "Stewie is a good guy. Stewie does no bad things";
s.find("(\\w+).*\\1") // will be true, and group 1 is the duplicated word. (note the additional java escape);

问题不清楚。谢谢你的回复,我有一个类似“title:Smith Vs Smith”的字符串。我需要一个正则表达式来检查字符串是否具有相同的名称,即Smith Vs Smith。仍然不清楚,请继续阅读
String s = "Stewie is a good guy. Stewie does no bad things";
s.find("(\\w+).*\\1") // will be true, and group 1 is the duplicated word. (note the additional java escape);