简体   繁体   中英

re.match (regular expression) in python: matching issue

I am trying to match some pattern (in a much bigger program). So my code right now is:

line = 'Name toyota;'
mo = re.match(r'(\w+)\s+([\w\s]+);', line)
print mo.group()

and this nicely prints: Name toyota;

but what if line can be anything. Examples: line = name SPU123-6CU; or line = name SPU123-389-2147656-1; or line = name SPU123-389-1213609-0_fuse; or line = name SPU123-1/0CN15; etc etc etc, how to make mo universal so irrespective of the string pattern in line , how can I have a single re.match statement and print out my output?

To extract the string after name.

mo = re.match(r'^(\w+)\s+(\S+);', line)

OR

mo = re.match(r'^(\w+)\s+([^;]*);', line)

And get the string you want from group index 2. [^;]* negated character class which matches any character but not of ; zero or more times. \\S+ matches any number of non-space characters.

>>> re.match(r'^(\w+)\s+([^;]*);',"name SPU123-389-1213609-0_fuse;").group(2)
'SPU123-389-1213609-0_fuse'
>>> re.match(r'^(\w+)\s+([^;]*);',"name SPU123-1/0CN15;").group(2)
'SPU123-1/0CN15'
>>> re.match(r'^name\s+([^;]*);',"name SPU123-1/0CN15;").group(1)
'SPU123-1/0CN15'
import re
line ="SPU123-6CU  fgfg"
mo = re.match(r'(?s).*', line)
print mo.group()

As has been point out by a few people:

line ="SPU123-6CU  fgfg"
print line

looks like what you really need

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