Pattern Search Regular Expression

Terry Reedy tjreedy at udel.edu
Sat Jun 15 16:24:15 EDT 2013


On 6/15/2013 12:28 PM, subhabangalore at gmail.com wrote:

> Suppose I want a regular expression that matches both "Sent from my iPhone" and "Sent from my iPod". How do I write such an expression--is the problem,
> "Sent from my iPod"
> "Sent from my iPhone"
>
> which can be written as,
> re.compile("Sent from my (iPhone|iPod)")
>
> now if I want to slightly to extend it as,
>
> "Taken from my iPod"
> "Taken from my iPhone"
>
> I am looking how can I use or in the beginning pattern?
>
> and the third phase if the intermediate phrase,
>
> "from my" if also differs or changes.
>
> In a nutshell I want to extract a particular group of phrases,
> where, the beginning and end pattern may alter like,
>
> (i) either from beginning Pattern B1 to end Pattern E1,
> (ii) or from beginning Pattern B1 to end Pattern E2,
> (iii) or from beginning Pattern B2 to end Pattern E2,

The only hints I will add to those given is that you need a) pattern for 
a word, and b) a way to 'anchor' the pattern to the beginning and ending 
of the string so it will only match the first and last words.

This is a pretty good re practice problem, so go and practice and 
experiment.  Expect to fail 20 times and you should beat your 
expectation ;-). The interactive interpreter, or Idle with its F5 Run 
editor window, makes experimenting easy and (for me) fun.

-- 
Terry Jan Reedy




More information about the Python-list mailing list