Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Pattern Search Regular Expression (http://www.velocityreviews.com/forums/t961793-pattern-search-regular-expression.html)

subhabangalore@gmail.com 06-15-2013 09:42 AM

Pattern Search Regular Expression
 
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

Steven D'Aprano 06-15-2013 10:05 AM

Re: Pattern Search Regular Expression
 
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?



--
Steven

Mark Lawrence 06-15-2013 10:18 AM

Re: Pattern Search Regular Expression
 
On 15/06/2013 10:42, subhabangalore@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


Denis McMahon 06-15-2013 10:24 AM

Re: Pattern Search Regular Expression
 
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.

--
Denis McMahon, denismfmcmahon@gmail.com

Mark Lawrence 06-15-2013 10:55 AM

Re: Pattern Search Regular Expression
 
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? :)

--
"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


rusi 06-15-2013 11:28 AM

Re: Pattern Search Regular Expression
 
On Jun 15, 3:55*pm, Mark Lawrence <breamore...@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]

Denis McMahon 06-15-2013 01:41 PM

Re: Pattern Search Regular Expression
 
On Sat, 15 Jun 2013 11:55:34 +0100, Mark Lawrence wrote:

> >>> 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? :)


Yeah - that last:

words[words[0],words[-1]]

should probably have been:

first_and_last = [words[0], words[-1]]

or even:

first_and_last = (words[0], words[-1])

Or even:

first_and_last = [sentence.split()[i] for i in (0, -1)]
middle = sentence.split()[1:-2]

--
Denis McMahon, denismfmcmahon@gmail.com

Denis McMahon 06-15-2013 01:45 PM

Re: Pattern Search Regular Expression
 
On Sat, 15 Jun 2013 13:41:21 +0000, Denis McMahon wrote:

> first_and_last = [sentence.split()[i] for i in (0, -1)] middle =
> sentence.split()[1:-2]


Bugger! That last is actually:

sentence.split()[1:-1]

It just looks like a two.

--
Denis McMahon, denismfmcmahon@gmail.com

Mark Lawrence 06-15-2013 02:28 PM

Re: Pattern Search Regular Expression
 
On 15/06/2013 14:45, Denis McMahon wrote:
> On Sat, 15 Jun 2013 13:41:21 +0000, Denis McMahon wrote:
>
>> first_and_last = [sentence.split()[i] for i in (0, -1)] middle =
>> sentence.split()[1:-2]

>
> Bugger! That last is actually:
>
> sentence.split()[1:-1]
>
> It just looks like a two.
>


I've a very strong sense of deja vu having round the same loop what, two
hours ago? Wondering out aloud the number of times a programmer has
thought "That's easy, I don't need to test it". How are the mighty fallen.

--
"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


subhabangalore@gmail.com 06-15-2013 02:31 PM

Re: Pattern Search Regular Expression
 
On Saturday, June 15, 2013 7:58:44 PM UTC+5:30, Mark Lawrence wrote:
> On 15/06/2013 14:45, Denis McMahon wrote:
>
> > On Sat, 15 Jun 2013 13:41:21 +0000, Denis McMahon wrote:

>
> >

>
> >> first_and_last = [sentence.split()[i] for i in (0, -1)] middle =

>
> >> sentence.split()[1:-2]

>
> >

>
> > Bugger! That last is actually:

>
> >

>
> > sentence.split()[1:-1]

>
> >

>
> > It just looks like a two.

>
> >

>
>
>
> I've a very strong sense of deja vu having round the same loop what, two
>
> hours ago? Wondering out aloud the number of times a programmer has
>
> thought "That's easy, I don't need to test it". How are the mighty fallen.
>
>
>
> --
>
> "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


Dear Group,

I know this solution but I want to have Regular Expression option. Just learning.

Regards,
Subhabrata.


All times are GMT. The time now is 06:46 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.