[Tutor] really basic py/regex

Peter Otten __peter__ at web.de
Sat Mar 31 06:00:10 EDT 2018


bruce wrote:

> Trying to quickly get the re.match(....)  to extract the groups from the
> string.
> 
> x="MATH 59900/40 [47490] - THE "
> 
> The regex has to return MATH, 59900, 40,, and 47490
> 
> d=re.match(r'(\D+)...) gets the MATH...
> 
> But I can't see (yet) how to get the rest of what I need...
> 
> Pointers would be useful.

Bruce, you have to make an attempt to explain exactly what you want in plain 
English. At the very least there have to be a few more examples, both 
matching and not matching.

The number of regular expressions that can be reverse-engineered from a 
single example is infinite, ranging from 

>>> re.compile("(MATH) (59900)/(40) \[(47490)\]").search(x).groups()
('MATH', '59900', '40', '47490')

-- which is most certainly too tight -- over

>>> re.compile(r"([A-Z]+)\s+(\d+)/(\d+)\s+\[(\d+)\]").search(x).groups()
('MATH', '59900', '40', '47490')

-- which may be just right -- to

>>> re.compile(r"([^ /[\]]+).*?([^ /[\]]+).*?([^ /[\]]+).*?([^ 
/[\]]+)").search(x).groups()
('MATH', '59900', '40', '47490')

-- which is probably too permissive.





More information about the Tutor mailing list