What is the difference between an anchored regex and an un-anchored regex?

后端 未结 1 540
忘了有多久
忘了有多久 2021-01-04 12:47

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

1条回答
  •  离开以前
    2021-01-04 13:08

    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.

    0 讨论(0)
提交回复
热议问题