Reputation: 561
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?
Upvotes: 0
Views: 68
Reputation: 174706
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'
Upvotes: 1
Reputation: 12801
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
Upvotes: 0