Pattern.COMMENTS 总是导致 Matcher.find 失败
以下代码匹配两个表达式并打印成功。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String regex = "\\{user_id : [0-9]+\\}";
String string = "{user_id : 0}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(string);
if (matcher.find())
System.out.println("Success.");
else
System.out.println("Failure.");
}
}
但是,我希望空格不重要,因此以下内容也应该打印成功。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String regex = "\\{user_id:[0-9]+\\}";
String string = "{user_id : 0}";
Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS);
Matcher matcher = pattern.matcher(string);
if (matcher.find())
System.out.println("Success.");
else
System.out.println("Failure.");
}
}
Pattern.COMMENTS 标志应该允许空白,但它会导致打印失败。如果字符串完全相同(包括空格),它甚至会导致打印失败,就像第一个示例一样。例如,
导入java.util.regex.Matcher; 导入java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String regex = "\\{user_id : [0-9]+\\}";
String string = "{user_id : 0}";
Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS);
Matcher matcher = pattern.matcher(string);
if (matcher.find())
System.out.println("Success.");
else
System.out.println("Failure.");
}
}
打印失败。
为什么会发生这种情况以及如何使模式忽略空白?
The following code matches the two expressions and prints success.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String regex = "\\{user_id : [0-9]+\\}";
String string = "{user_id : 0}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(string);
if (matcher.find())
System.out.println("Success.");
else
System.out.println("Failure.");
}
}
However, I want white space to not matter, so the following should also print success.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String regex = "\\{user_id:[0-9]+\\}";
String string = "{user_id : 0}";
Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS);
Matcher matcher = pattern.matcher(string);
if (matcher.find())
System.out.println("Success.");
else
System.out.println("Failure.");
}
}
The Pattern.COMMENTS flag is supposed to permit white space, but it causes Failure to be printed. It even causes Failure to be printed if the strings are exactly equivalent including white space, like in the first example. For example,
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String regex = "\\{user_id : [0-9]+\\}";
String string = "{user_id : 0}";
Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS);
Matcher matcher = pattern.matcher(string);
if (matcher.find())
System.out.println("Success.");
else
System.out.println("Failure.");
}
}
Prints Failure.
Why is this happening and how do I make the Pattern ignore white space?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你这边有误会。
Pattern.COMMENTS
允许您在正则表达式中添加额外的空格,以提高正则表达式的可读性,但该空格不会在字符串中匹配。这不允许字符串中存在空格,然后自动匹配空格,而无需在正则表达式中定义。
示例
使用
Pattern.COMMENTS
,您可以像这样在正则表达式中添加空格以提高可读性,但它不会匹配字符串
,因为您尚未在正则表达式中定义空格字符串,所以如果你想使用
Pattern.COMMENTS
那么你需要特殊对待你想要匹配的空白,要么你转义它要么你使用空白类
There is a misunderstanding on your side.
Pattern.COMMENTS
allow you to put additional whitespace into your regex, to improve the readability of the regex, but this whitespace will NOT be matched in the string.This does not allow whitespace in your string, that is then matched automatically, without being defined in the regex.
Example
With
Pattern.COMMENTS
you can put whitespace in your regex like thisto improve readablitiy, but the it will not match the string
because you haven't defined the whitespaces in the string, so if you want to use
Pattern.COMMENTS
then you need to treat whitespace you want to match specially, either you escape itor you use the whitespace class