简体   繁体   English

非常简单的Java正则表达式没有给出预期的结果

[英]Very simple Java regex not giving expected result

Today is my first day learning regular expressions (literally no background before this) through the chapter Strings in the book Thinking in Java 4th Edition. 今天是我第一天通过Thinking in Java 4th Edition中的Strings章节学习正则表达式(在此之前没有背景)。 I am pulling my hair out as to why the regular expression is not matching any region of the input string. 我正在拉我的头发,为什么正则表达式不匹配输入字符串的任何区域。 I have tested this in regex101 and I get the result I expected, but in Java (which you can't test on the regex101 site admittedly) the result is different. 我在regex101中对此进行了测试,得到了我期望的结果,但是在Java中(你无法在regex101网站上测试),结果是不同的。
EDIT: Doing exercise 10 in the chapter 编辑:在本章中进行练习10

Regex: nw\\s+h(a|i)s 正则表达式: nw\\s+h(a|i)s
Input String: Java now has regular expressions 输入字符串: Java now has regular expressions
Expected Result: A match found in the region "now has" of the input string 预期结果:在输入字符串的"now has"区域中找到匹配项
Actual Result: No match found 实际结果:未找到匹配项

My relevant code: 我的相关代码:

import java.util.regex.*;

public class Foo {
  public static void main(String[] args) {
    // NOTE: I've also tested passing the regex as an arg from the command line 
    //       as "n.w\s+h(a|i)s"
    String regex = "n.w\\s+h(a|i)s";
    String input = "Java now has regular expressions";

    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(input);

    // Starting at the beginning of the input string, look for a match in ANY 
    // region of the input string
    boolean matchFound = m.lookingAt();
    System.out.println("Match was found: " + matchFound);
  }
}
/* OUTPUT
-> Match was found: false
*/

Use boolean matchFound = m.find(); 使用boolean matchFound = m.find(); instead of boolean matchFound = m.lookingAt(); 而不是boolean matchFound = m.lookingAt();

From Javadocs 来自Javadocs

lookingAt() tries to match the input sequence, starting at the beginning of the region, against the pattern. lookingAt()尝试lookingAt()区域开头开始的输入序列与模式匹配。

Use m.find() instead of m.lookingAt() 使用m.find()而不是m.lookingAt()

You can print what you get by m.group() 您可以打印m.group()获得的m.group()

Please check code below. 请检查下面的代码。

import java.util.regex.*;

public class Foo {
    public static void main(String[] args) {
        // NOTE: I've also tested passing the regex as an arg from the command
        // line
        // as "n.w\s+h(a|i)s"
        String regex = "n.w\\s+h(a|i)s";
        String input = "Java now has regular expressions";

        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(input);

        // Starting at the beginning of the input string, look for a match in
        // ANY
        // region of the input string
        boolean matchFound = m.find();
        System.out.println("Match was found: " + matchFound);
        System.out.println("Matched string is: " + m.group());
    }
}

The javadoc of lookingAt() is lookingAt()的javadoc是

public boolean lookingAt() public boolean lookingAt()

Attempts to match the input sequence, starting at the beginning of the region, against the pattern. 尝试将输入序列(从区域的开头开始)与模式匹配。 Like the matches method, this method always starts at the beginning of the region; 与匹配方法一样,此方法始终从区域的开头开始; unlike that method, it does not require that the entire region be matched. 与该方法不同,它不需要匹配整个区域。

If the match succeeds then more information can be obtained via the start, end, and group methods. 如果匹配成功,则可以通过start,end和group方法获得更多信息。

Returns:true if, and only if, a prefix of the input sequence matches this matcher's pattern 返回:当且仅当输入序列的前缀与此匹配器的模式匹配时才返回true

That means, this method expects the regex matches at the very beginning of input String. 这意味着,此方法需要在输入String的最开头处使用正则表达式匹配。

This method is not used frequently, the effect is like you modify your regex to "^nw\\\\s+h(a|i)s" , and use find() method. 此方法不经常使用,效果就像您将正则表达式修改为"^nw\\\\s+h(a|i)s" ,并使用find()方法。 It also puts a limitation that the regex matches at the very beginning of the input String. 它还给出了正则表达式在输入String的最开头匹配的限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM