matcher.find() 是 Java 中的正则表达式函数。

huangapple go评论87阅读模式
英文:

matcher.find() regex Java function

问题

以下是翻译好的部分:

"考虑下面的代码,该代码检查输入是否具有以下形式:<tag>contents</tag>。如果输入处于上述形式中,则打印标签之间的内容,否则打印“None”。

例如:
1)输入:<h1>Nayeem loves counseling</h1>
输出:Nayeem loves counseling

2)输入:<h1><h1>Sanjay has no watch</h1></h1><par>So wait for a while</par>
输出:Sanjay has no watch
So wait for a while

3)输入:<Amee>safat codes like a ninja</amee>
输出:None

String line = scan.nextLine();

boolean matchFound = false;
Pattern r = Pattern.compile("<(.+)>([^<]+)</\\1>");
Matcher m = r.matcher(line);

while(m.find()) {
    System.out.println(m.group(2));
    matchFound = true;
}

if(!matchFound) {
    System.out.println("None");
}

这里使用find()函数,find()返回布尔类型。我的问题是为什么应该在while循环中使用find()?为什么不能在if语句中使用它?find()在内部是如何工作的?"

英文:

consider the below code that checks if input is in the form: <tag>contents</tag>
If input in above form, print content between the tags else print None.

eg:

  1. input: <h1>Nayeem loves counseling</h1>
    output: Nayeem loves counseling

  2. input: <h1><h1>Sanjay has no watch</h1></h1><par>So wait for a while</par>
    output: Sanjay has no watch
    So wait for a while

  3. input: <Amee>safat codes like a ninja</amee>
    output: None

String line = scan.nextLine();

boolean matchFound = false;
Pattern r = Pattern.compile("<(.+)>([^<]+)</\\1>");
Matcher m = r.matcher(line);

while(m.find()) {
    System.out.println(m.group(2));
    matchFound = true;
}

if(!matchFound) {
    System.out.println("None");
}

Here find() returns boolean type. My question is why should find() be used with while loop? Why can't I use "if" in place of while? How is find working internally?

答案1

得分: 1

当你调用find()时,匹配器将尝试查找与你的正则表达式模式匹配的下一个序列。通过在while循环中使用find(),你将能够获取所有匹配的序列(如果有的话)。如果你想将输入字符串作为整体进行匹配,可以使用matches()

参考链接:

Java文档中的find方法

Java文档中的matches方法

英文:

When you call find(), the matcher will attempt to find the next sequence that matches your regex pattern. By using find() in a while loop, you will be able to obtain all the matching sequences if there are any. If you want to match the input string as a whole, you can use matches().

Reference:

find method in Java Docs

matches method in Java Docs

答案2

得分: 0

答案在findjavadoc中。

public boolean find()

尝试查找与模式匹配的输入序列的下一个子序列。

该方法从此匹配器的区域的开头开始,或者如果先前的方法调用成功并且匹配器自那时以来未被重置,则从先前匹配的匹配之后第一个不匹配的字符开始

如果匹配成功,则可以通过start、end和group方法获取更多信息。

您正在查看的代码是重复查找正则表达式的下一个匹配项,并打印其匹配的第二组。由于匹配的第二组是标签内容,因此这会打印所有标签的内容。

如果您使用if而不是while,则只会打印第一个匹配项;即只会打印第一个标签的内容。

英文:

The answer is in the javadoc for find.

> public boolean find()
>
> Attempts to find the next subsequence of the input sequence that matches the pattern.
>
> This method starts at the beginning of this matcher's region, or, if a previous invocation of the method was successful and the matcher has not since been reset, at the first character not matched by the previous match.
>
> If the match succeeds then more information can be obtained via the start, end, and group methods.

The code you are looking at is repeatedly finding the next match for the regex and printing its matched 2nd group. Since the 2nd group of the match is the tag contents, this prints the contents of all tags.

If you used if instead of while, only the first match will be printed; i.e. only content of the first tag.

huangapple
  • 本文由 发表于 2020年8月5日 21:09:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63265918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定