[triangle-zpug] how to match strings in python

Stephan Altmueller stephan_altmueller at unc.edu
Thu Apr 2 15:49:00 UTC 2009


Hi,

Regular expressions are the way to go if you want to find a particular
string.

But if I understand the initial question correctly you want to find the
longest
common substring that two string share, correct ?

That's a well defined computer science problem:

http://en.wikibooks.org/wiki/Algorithm_implementation/Strings/Longest_common_substring#Python

and here is an implementation in Python:

http://en.wikibooks.org/wiki/Algorithm_implementation/Strings/Longest_common_substring

I haven't tried the code, so your mileage my vary.

  -- Stephan


Clark, Tom wrote:
> Joe,
>
> 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:
>
> 	m/fooba.*/  
>
> with search/replace, assignment to variables etc. available as you need
> them.  
>
> 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
> it.
>
> Love to hear others opinions.
>
> TC
>
>
> Tom Clark, MLS
> Director, Wiggins Library
> Shaw University Divinity School
> 919-716-5518
> 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
> GNU/Linux!
>
> _______________________________________________
> triangle-zpug mailing list
> triangle-zpug at starship.python.net
> http://starship.python.net/mailman/listinfo/triangle-zpug
>
> _______________________________________________
> triangle-zpug mailing list
> triangle-zpug at starship.python.net
> http://starship.python.net/mailman/listinfo/triangle-zpug
>   


-- 
-------------------------------------------------
Stephan Altmueller
Applications Analyst, Enterprise Applications
Office of Arts and Sciences Information Services
University of North Carolina at Chapel Hill
CB 3056, 06 Howell Hall
Chapel Hill, NC 27599-3056 
919.448.5936 (direct line)
stephan_altmueller at unc.edu 
AIM: oasisaltmuell
http://oasis.unc.edu




More information about the triangle-zpug mailing list