I am trying to use some regex in python for pattern matching. I am looking at a very simple example, but this does not work as I expected. the snippet is as follows. I expected it to print "match", but it did not
>>> line="123 a bcdef12"
>>> data_headers = re.compile('.*a bc.* ')
>>> if data_headers.match(line):
... print "match"
...
>>>
I also tried the following:
>>> data_headers = re.compile(' a bc* ')
>>> data_headers = re.compile('.*a bc* ')
but both did not find any match.
Any suggestions are welcome. Thanks
The space at the end is what's stopping it from matching:
>>> import re
>>> re.match(".*a bc.* ", "123 a bcdef12")
None
>>> re.match(".*a bc.*", "123 a bcdef12")
<_sre.SRE_Match object at 0x7fdd6c462b90>
You might find a tool such as debuggex (there are many others) useful for testing and debugging regex expressions.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.