Pattern Search Regular Expression

rusi rustompmody at gmail.com
Sat Jun 15 07:28:45 EDT 2013


On Jun 15, 3:55 pm, Mark Lawrence <breamore... at yahoo.co.uk> wrote:
> On 15/06/2013 11:24, Denis McMahon wrote:
>
>
>
>
>
>
>
>
>
> > On Sat, 15 Jun 2013 10:05:01 +0000, Steven D'Aprano wrote:
>
> >> On Sat, 15 Jun 2013 02:42:55 -0700, subhabangalore 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.
>
> >> No need for a regular expression.
>
> >> py> sentence = "By the new group"
> >> py> words = sentence.split()
> >> py> words[1:-1]
> >> ['the', 'new']
>
> >> Does that help?
>
> > I thought OP wanted:
>
> > words[words[0],words[-1]]
>
> > But that might be just my caffeine deprived misinterpretation of his
> > terminology.
>
>  >>> sentence = "By the new group"
>  >>> words = sentence.split()
>  >>> words[words[0],words[-1]]
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> TypeError: list indices must be integers, not tuple
>
> So why would the OP want a TypeError?  Or has caffeine deprivation
> affected your typing skills? :)

:-)

I guess Denis meant (words[0], words[-1])

To the OP:
You have the identity:
words == [words[0]] + words[1:-1] + [words[-1]]

So take your pick of what parts of the expression you want (and
discard what you dont want).
[The way you've used 'extract' is a bit ambiguous]



More information about the Python-list mailing list