[triangle-zpug] how to match strings in python

Clark, Tom tomclark at shawu.edu
Thu Apr 2 14:59:35 UTC 2009


I believe this is a job for Regular Expressions, which is it's own thing
independent of Python, though Python supports it.

I'm only just learning, but the RE (or Regex) syntax would go something
like this:


with search/replace, assignment to variables etc. available as you need

As has famously been said, if you decide to use RE to solve a
programming problem, you now have two programming problems.  OTOH, if
you do a lot of text manipulations, I don't think there's any way around

Love to hear others opinions.


Tom Clark, MLS
Director, Wiggins Library
Shaw University Divinity School
tomclark at shawu.edu

-----Original Message-----
From: triangle-zpug-bounces at starship.python.net
[mailto:triangle-zpug-bounces at starship.python.net] On Behalf Of Joseph
Mack NA3T
Sent: Thursday, April 02, 2009 9:15 AM
To: zpug
Subject: [triangle-zpug] how to match strings in python

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

triangle-zpug mailing list
triangle-zpug at starship.python.net

More information about the triangle-zpug mailing list