How Regex engine parse anchors -


this question has answer here:

can explain how regex engine works when tries match

  ^4$ 749\n486\n4  

i mean how regex engine parse string while performing match

the regexp ^4$ means match line contains digit 4

if apply regexp string contains newline characters treat first character of string start of line , first newline end of line. additional characters after newline ignored. example in perl

  db<1> $str="749\n486\n4";   db<2> x $str =~ /^4$/   empty array 

example in python

>>> import re >>> s="749\n486\n4" >>> re.search('^4$',s) 

however, regexp implementations have way of dealing this. there multiline setting. in perl

  db<3> x $str =~ /^4$/m 0  1 

in python

>>> re.search('^4$',s,re.multiline) <_sre.sre_match object @ 0x7f446874b030> 

the python docs explain multiline mode this

re.multiline when specified, pattern character '^' matches @ beginning of string , @ beginning of each line (immediately following each newline); , pattern character '$' matches @ end of string , @ end of each line (immediately preceding each newline). default, '^' matches @ beginning of string, , '$' @ end of string , before newline (if any) @ end of string.

if in multiline string wanted know if ended in digit 4 on single line there syntax feature this

  db<4> x $str =~ /^4\z/m 0  1 

see http://perldoc.perl.org/perlre.html on m flag , \a, \z, \z or http://docs.python.org/2/library/re.html#regular-expression-objects


Comments

Popular posts from this blog

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

Android Gson.fromJson error -