[triangle-zpug] how to match strings in python
sampbrauer at yahoo.com
Thu Apr 2 15:50:09 UTC 2009
Sometimes I love little exercises like this. Here's a function that does what you want just using basic Python.
def getMatchingSubstring(s1, s2):
if len(s1) < len(s2):
shorter = s1
other = s2
shorter = s2
other = s1
if other.startswith(shorter): return shorter
i = 0
while(i < len(shorter)):
if shorter[i] != other[i]: break
i += 1
--- On Thu, 4/2/09, Joseph Mack NA3T <jmack at wm7d.net> wrote:
> From: Joseph Mack NA3T <jmack at wm7d.net>
> Subject: [triangle-zpug] how to match strings in python
> To: "zpug" <triangle-zpug at starship.python.net>
> Date: Thursday, April 2, 2009, 9:15 AM
> 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
> -- Joseph Mack NA3T EME(B,D), FM05lw North Carolina
> jmack (at) wm7d (dot) net - azimuthal equidistant map
> generator at http://www.wm7d.net/azproj.shtml
> Homepage http://www.austintek.com/ It's GNU/Linux!
> triangle-zpug mailing list
> triangle-zpug at starship.python.net
More information about the triangle-zpug