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.
我是正则表达式的新手。我以为这会返回matched succesfully,但事实并非如此。为什么呢?
matched succesfully
String myString = "SUB_HEADER5_LABEL"; if (myString.matches(Pattern.quote("SUB_HEADER?_LABEL"))) { System.out.println("matched succesfully"); }
Pattern.qoute()将创建一个仅与给定字符串完全匹配的模式。你需要
Pattern.qoute()
if (myString.matches("SUB_HEADER\\d_LABEL"))
如果您希望数字超过 9,请添加+量词,例如
+
if (myString.matches("SUB_HEADER\\d+_LABEL"))
如果您想匹配您拥有的数字?(在您的情况下意味着一或零R,因为它是一个量词)。您需要将其替换为[0-9]or \\d。
?
R
[0-9]
\\d