Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有:
Pattern pat = Pattern.compile("(\\d+) (\\d+) (1$)"); Matcher mat = pat.matcher(line);
匹配:
1 2 1
但不适用于:
我怎样才能实现,模式匹配根据数字之间的空格是不敏感的?
用于\s一个空格并添加一个+表示一个或多个空格。
\s
+
"(\\d+)\\s+(\\d+)\\s+(1$)"
如果您想要零个或多个空格,则必须使用 a*而不是+.
*
与空格一起使用quantifier (+),以匹配one or more空格:-
quantifier (+)
one or more
Pattern.compile("(\\d+)\\s+(\\d+)\\s+(1$)");
同样还有其他quantifiers: -
quantifiers
0 or more
?
0 or 1