0

我有以下 reg ex 并且它无法匹配并设置为 true?

String whatever = "> blah, blah, blah";
boolean q = Pattern.matches(whatever, "^>+");  // this evaluates to false

我在字符串上正确匹配吗?我错过了什么?谢谢!

4

1 回答 1

3

"^>+"将匹配一个或多个的序列>。要匹配以 开头的字符串>,请使用:

whatever.matches(">.+");  // .+ after >

使用String#matches()方法而不是Pattern.matches(). 您的方法中的参数顺序不正确。Pattern.matches()方法将正则表达式作为它的第一个参数。您将它作为第二个参数传递。

请注意,在使用该matches()方法时使用正则表达式时,锚是隐式的。您无需明确提供它们。

于 2013-10-07T22:35:04.847 回答