[triangle-zpug] how to match strings in python
Josh Johnson
josh_johnson at unc.edu
Thu Apr 2 14:19:20 UTC 2009
Joseph Mack NA3T wrote:
> I've looked in the string methods/functions in the python docs and I
> can't see how to do what I want, which is to find the parts of strings
> that match. eg
>
>
> string_1 = "foobar"
> string_2 = "foobaz"
>
> matched_string = "fooba"
>
> I need to walk along the string(s) 1 char at a time, accepting
> matching letters, till I get a mismatch, when the code exits. I was
> expecting to be able to retrieve chars one at a time from each of the
> two strings and test if the chars were the same.
>
> How do I do this in python?
>
> Thanks Joe
>
you can loop over a string like a list, like this:
for c in string1:
for m in matched string:
if c != m:
break
(untested, but the general idea)
Have you seen the 're' module? What you're talking about is basically
what regular expressions do in the background :)
JJ
More information about the triangle-zpug
mailing list