Pattern.matches不起作用,而replaceAll则不起作用

Gab*_*e B 7 java regex

正则表达式似乎没问题,因为第一行代码正确地用"helloworld"替换子字符串,但是同一个表达式在后者中不匹配,因为我看不到"whynothelloworld?" 在控制台上

System.out.println(current_tag.replaceAll("^[01][r]\\s", "helloworld"));

if (Pattern.matches("^[01][r]\\s", current_tag)) { System.out.println("whynothelloworld?");}
Run Code Online (Sandbox Code Playgroud)

Tim*_*ker 15

Pattern.matches()期望整个字符串匹配,而不仅仅是子字符串.

.find()改为使用正则表达式匹配器对象的方法:

Pattern regex = Pattern.compile("^[01]r\\s");
Matcher regexMatcher = regex.matcher(current_tag);
foundMatch = regexMatcher.find();
Run Code Online (Sandbox Code Playgroud)