20

In regular expressions:

  • What is the difference between ^ and \A?
  • What is the difference between $ and \Z?
Matt Ball
  • 332,322
  • 92
  • 617
  • 683
Rohit Chauhan
  • 561
  • 5
  • 17

5 Answers5

15

In single-line mode, $ matches either the end of the string, or just before the newline at the end of the string. In multiline mode $ matches before each newline in the string. \Z always matches only the end of the string regardless of the line mode. Same with ^ versus \A.

Ignacio Vazquez-Abrams
  • 699,552
  • 132
  • 1,235
  • 1,283
  • 2
    sorry but that is not quite right. `\Z` matches the end of the string **OR** one before the end of the string if the last char is a `\n`. For the absolutely, non-oh-just-in-case version, you want `\z` (little zed not BIG ZED), which is guaranteed to match only if there are no chars left. In some regex languages, BTW, what counts as a newline may be flexible; if you’re lucky it will match `(?:(?>\r\n)|\v)` for any linebreak sequence, where `\v` is the Unicode `\p{Vertical_Space}` property. – tchrist Nov 22 '10 at 21:09
  • One other thing: single-line mode is *not* the opposite of multiline mode; they're completely separate. Multiline mode alters the behavior of `^` and `$` as you described, while single-line mode allows `.` to match line-separator characters. – Alan Moore Nov 22 '10 at 21:45
  • 1
    And a note to the tchrist's comment: in Python `re`, `\Z` is equal to `\z` in Perl/.NET/Java/PCRE. – Wiktor Stribiżew Aug 20 '16 at 15:13
8

See perldoc perlre.

The \A and \Z are just like "^" and "$", except that they won't match multiple times when the /m modifier is used, while "^" and "$" will match at every internal line boundary. To match the actual end of the string and not ignore an optional trailing newline, use \z .

Eugene Yarmash
  • 119,667
  • 33
  • 277
  • 336
3

They are different when it comes to matching string with multiple lines.

^ can match at the start of the string and after each line break. \A only ever matches at the start of the string

$ can match at the end of the string and before each line break. \Z only ever matches at the end of the string.

simshaun
  • 20,601
  • 1
  • 51
  • 69
0

They are different indeed.

import re

#A. Match only single line examples
print(re.findall('^B.+d$', 'Beginning to end'))
#['Beginning to end'

print(re.findall('\AB.+d$', 'Beginning to end'))
#['Beginning to end']


#B. Match multiple lines but MULTILINE option not enabled examples
print(re.findall('^B.+d$', 'Beginning to end\nBusy street end\nBeer nuts and almond'))
#

print(re.findall('\AB.+d$', 'Beginning to end\nBusy street end\nBeer nuts and almond'))
#

#C. Match multiple lines with MULTILINE option enabled (^ and \A with re.M) examples
print(re.findall('^B.+d$', 'Beginning to end\nBusy street end\nBeer nuts and almond', re.M))
#['Beginning to end', 'Busy street end', 'Beer nuts and almond']

print(re.findall('\AB.+d$', 'Beginning to end\nBusy street end\nBeer nuts and almond', re.M))
#['Beginning to end']


#D. \Z with ^ and \A with re.M examples
print(re.findall('^B.+d\Z', 'Beginning to end\nBusy street end\nBeer nuts and almond', re.M))
#['Beer nuts and almond']

print(re.findall('\AB.+d\Z', 'Start to finish\nSpecial fish\nSuper fresh', re.M))
#
0

An additional example in Python because the above ones are a bit loaded. In Python when using MULTILINE mode ^ and $ will also match before each linebreak, whilst when using /A and /Z it will only match begin/end of the string, respectively.

import re

string = "Test test test xzz\nTest 123 Test xzz"
#Test test test xzz
#Test 123 Test xzz

#Multiline mode
re.findall(r'xzz$', string, re.MULTILINE)
#['xzz', 'xzz']

re.findall(r'xzz\Z', string, re.MULTILINE)
#['xzz']

#No multiline mode
re.findall(r'xzz$', string)
#['xzz']

re.findall(r'xzz\Z', string)
#['xzz']
user21398
  • 345
  • 4
  • 13