When should I use \A in a regex?

前端 未结 4 814
暗喜
暗喜 2021-02-19 05:32

End of line anchor $ match even there is extra trailing \\n in matched string, so we use \\Z instead of $

For example

相关标签:
4条回答
  • 2021-02-19 05:39

    Most often it's used when also enabling multi-line matches. Since \A only matches at the beginning of the ENTIRE text, as opposed to just a line beginning, in regexes that can match across lines the functionality of ^ and \A are different.

    0 讨论(0)
  • 2021-02-19 05:41

    Not directly relevant to your question according to the tags you used, but there is at least one language (Ruby) where ^ and $ always mean start/end-of-line, so if you want to match start/end-of-string you have to use \A and \Z or \z.

    If you want to keep your regexes portable, it's good practice to explicitly state what you want them to do instead of relying on the availability of mode modifiers like \m or Regex.MULTILINE etc.

    On the other hand, JavaScript, POSIX and XML do not support \A and \Z. This is where tools like RegexBuddy come in handy that translate regexes from one flavor to the other for you.

    0 讨论(0)
  • 2021-02-19 05:41

    If the regex flavor you're working with supports \A then I recommend you always use it instead of ^. \A always matches at the start of the string only in all flavors that support it. There is no issue with line breaks.

    ^ may match at the start of the string only or at the start of any line depending on the regex flavor and regex options.

    By using \A you reduce the potential for confusion when somebody else has to maintain your code.

    0 讨论(0)
  • 2021-02-19 05:49

    As with any regex feature, you use it when it more exactly describes what you need as opposed to any more general feature. If you know that you want to match exactly at the start of a string (instead of logical lines), use the regex feature that describes that. Don't use regex features that could possibly match in situations that you don't want.

    For Perl, see the perlre docs for details about the zero-width assertions:

    \b  Match a word boundary
    \B  Match except at a word boundary
    \A  Match only at beginning of string
    \Z  Match only at end of string, or before newline at the end
    \z  Match only at end of string
    \G  Match only at pos() (e.g. at the end-of-match position
        of prior m//g)
    
    0 讨论(0)
提交回复
热议问题