简体   繁体   中英

Python regex equivalent for perl

what is the equivalent of following Perl condition in Python

if($line=~/DramBase/)

I tried the following but it didn't match(the line at the bottom)

if(re.match( r'DramBase', line)):

I had to change it to

if(re.match( r'.*DramBase', line)):

to match this line

# -DF0.CCM0.DramBaseAddress1 0x00004001

Is there a flag to match it anywhere on the line without explicitly matching starting characters ?

You need to use re.search , not re.match . re.match only matches as the beginning of the string, while re.search matches anywhere, like in Perl.

See re — Regular expression operations for an explanation

re.match(pattern, string, flags=0)

If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding match object. Return None if the string does not match the pattern; note that this is different from a zero-length match.

Note that even in MULTILINE mode, re.match() will only match at the beginning of the string and not at the beginning of each line.

If you want to locate a match anywhere in string, use search() instead (see also search() vs. match()).

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM