checking a string against multiple patterns
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Tue Dec 18 08:05:43 EST 2007
On 18 dic, 09:41, tomasz <tmkm... at googlemail.com> wrote:
> Hi,
>
> here is a piece of pseudo-code (taken from Ruby) that illustrates the
> problem I'd like to solve in Python:
>
> str = 'abc'
> if str =~ /(b)/ # Check if str matches a pattern
> str = $` + $1 # Perform some action
> elsif str =~ /(a)/ # Check another pattern
> str = $1 + $' # Perform some other action
> elsif str =~ /(c)/
> str = $1
> end
>
> The task is to check a string against a number of different patterns
> (containing groupings).
> For each pattern, different actions need to be taken.
>
> In Python, a single match of this kind can be done as follows:
>
> str = 'abc'
> match = re.search( '(b)' , str )
> if match: str = str[0:m.start()] + m.group(1) # I'm not sure if
> this way of accessing 'pre-match'
> # is
> optimal, but let's ignore it now
>
> The problem is that you you can't extend this example to multiple
> matches with 'elif'
> because the match must be performed separately from the conditional.
>
> This obviously won't work in Python:
>
> if match=re.search( pattern1 , str ):
> ...
> elif match=re.search( pattern2 , str ):
> ...
>
> So the only way seems to be:
>
> match = re.search( pattern1 , str ):
> if match:
> ....
> else:
> match = re.search( pattern2 , str ):
> if match:
> ....
> else:
> match = re.search( pattern3 , str ):
> if match:
> ....
>
> and we end up having a very nasty, multiply-nested code.
Define a small function with each test+action, and iterate over them
until a match is found:
def check1(input):
match = re.search(pattern1, input)
if match:
return input[:match.end(1)]
def check2(input):
match = re.search(pattern2, input)
if match:
return ...
def check3(input):
match = ...
if match:
return ...
for check in check1, check2, check3:
result = check(input)
if result is not None:
break
else:
# no match found
--
Gabriel Genellina
More information about the Python-list
mailing list