Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If there difference between `\A` vs `^` (caret) in regular expression?

Python's re module documentation says:

^: (Caret.) Matches the start of the string, and in MULTILINE mode also matches immediately after each newline.

\A: Matches only at the start of the string.

Is there any difference when using them?

like image 987
Jianxin Gao Avatar asked Sep 23 '16 20:09

Jianxin Gao


1 Answers

Both of these match:

re.search('^abc', 'abc')
re.search('\Aabc', 'abc')

This also matches:

re.search('^abc', 'firstline\nabc', re.M)

This does not:

re.search('\Aabc', 'firstline\nabc', re.M)
like image 159
Martin Drohmann Avatar answered Sep 29 '22 12:09

Martin Drohmann