What is the difference between an anchored regex and an un-anchored regex?
Usage found here:
... These should be specified as a list of pair
Unanchored regex means a regex pattern that has no anchors, ^
for start of string, and $
for the end of string, and thus allows partial matches. E.g. in Java, Matcher#find()
method can search for partial matches inside an input string, "a.c"
will find a match in "1.0 abc."
. The anchored "^a.c$"
pattern will match an "abc"
string, but won't find a match in "1.0 abc."
.
Also, an unanchored regex may mean the code that handles the regex pattern does not check if the match is equal to full input string. E.g. in Java, Matcher#matches()
method requires that the pattern must match the full input string and s.matches("a.c")
will match an "abc"
string, but won't find a match in "1.0 abc."
.
Anchored regex means the pattern will only match a string if the whole string matches.
See Start of String and End of String Anchors for more information about anchors in regex.