Pattern Search Regular Expression

Mark Lawrence breamoreboy at yahoo.co.uk
Sat Jun 15 06:18:10 EDT 2013


On 15/06/2013 10:42, subhabangalore at gmail.com wrote:
> Dear Group,
>
> I am trying to search the following pattern in Python.
>
> I have following strings:
>
>   (i)"In the ocean"
>   (ii)"On the ocean"
>   (iii) "By the ocean"
>   (iv) "In this group"
>   (v) "In this group"
>   (vi) "By the new group"
>         .....
>
> I want to extract from the first word to the last word,
> where first word and last word are varying.
>
> I am looking to extract out:
>    (i) the
>    (ii) the
>    (iii) the
>    (iv) this
>    (v) this
>    (vi) the new
>        .....
>
> The problem may be handled by converting the string to list and then
> index of list.
>
> But I am thinking if I can use regular expression in Python.
>
> If any one of the esteemed members can help.
>
> Thanking you in Advance,
>
> Regards,
> Subhabrata
>

I tend to reach for string methods rather than an RE so will something 
like this suit you?

c:\Users\Mark\MyPython>type a.py
for s in ("In the ocean",
           "On the ocean",
           "By the ocean",
           "In this group",
           "In this group",
           "By the new group"):
     print(' '.join(s.split()[1:-1]))


c:\Users\Mark\MyPython>a
the
the
the
this
this
the new

-- 
"Steve is going for the pink ball - and for those of you who are 
watching in black and white, the pink is next to the green." Snooker 
commentator 'Whispering' Ted Lowe.

Mark Lawrence




More information about the Python-list mailing list