What is the difference between an anchored regex and an un-anchored regex?
Usage found here:
... These should be specified as a list of pairs where the first element is an un-anchored regex (in java.util.regex.Pattern syntax) against which the platform name is matched...
Anchors belong to the family of regex tokens that don't match any characters, but that assert something about the string or the matching process. Anchors assert that the engine's current position in the string matches a well-determined location: for instance, the beginning of the string, or the end of a line.
There are also two types of regular expressions: the "Basic" regular expression, and the "extended" regular expression.
String regex = "\\."; Notice that the regular expression String contains two backslashes after each other, and then a . . The reason is, that first the Java compiler interprets the two \\ characters as an escaped Java String character. After the Java compiler is done, only one \ is left, as \\ means the character \ .
A regular expression is a pattern that the regular expression engine attempts to match in input text. A pattern consists of one or more character literals, operators, or constructs.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With