In regular expressions:
- What is the difference between
^
and\A
? - What is the difference between
$
and\Z
?
In regular expressions:
^
and \A
?$
and \Z
?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
.
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 .
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.
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']
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))
#