Are \A \z in String#matches redundant?

user1589188 :

In java String class the matches tests if a string matches a regex pattern. So obviously it is the entire string against the regex pattern, does that mean \A, \z, ^, $ are redundant here?

If not, please provide examples to show why they are not redundant in this case.

4castle :

They're redundant unless you activate multiline mode, in which case ^ and $ would match the beginning and end of individual lines instead of the whole input.

You can activate multiline mode while using String#matches by using the embedded flag expression (?m) inside the regex (usually at the beginning).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=97370&siteId=1