The regular expression seems to be ok, since the first line line correctly replace the substring with “helloworld”, but the same expression won’t match in the latter since i cannot see “whynothelloworld?” on console
System.out.println(current_tag.replaceAll("^[01][r]\\s", "helloworld"));
if (Pattern.matches("^[01][r]\\s", current_tag)) { System.out.println("whynothelloworld?");}
Pattern.matches()expects the entire string to match, not just a substring.Use the
.find()method of the regex matcher object instead: