Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Pattern Search Regular Expression

Reply
Thread Tools

Pattern Search Regular Expression

 
 
subhabangalore@gmail.com
Guest
Posts: n/a
 
      06-15-2013
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
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      06-15-2013
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
 
Reply With Quote
 
 
 
 
Mark Lawrence
Guest
Posts: n/a
 
      06-15-2013
On 15/06/2013 10:42, http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
Denis McMahon
Guest
Posts: n/a
 
      06-15-2013
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, (E-Mail Removed)
 
Reply With Quote
 
Mark Lawrence
Guest
Posts: n/a
 
      06-15-2013
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

 
Reply With Quote
 
rusi
Guest
Posts: n/a
 
      06-15-2013
On Jun 15, 3:55*pm, Mark Lawrence <(E-Mail Removed)> 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]
 
Reply With Quote
 
Denis McMahon
Guest
Posts: n/a
 
      06-15-2013
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, (E-Mail Removed)
 
Reply With Quote
 
Denis McMahon
Guest
Posts: n/a
 
      06-15-2013
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, (E-Mail Removed)
 
Reply With Quote
 
Mark Lawrence
Guest
Posts: n/a
 
      06-15-2013
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

 
Reply With Quote
 
subhabangalore@gmail.com
Guest
Posts: n/a
 
      06-15-2013
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
need regular expression to replace part of result based on a search pattern Jimmy Java 13 07-25-2012 09:37 PM
Search regular expression with search for hex values in files? Peter Hanke Perl Misc 1 01-06-2008 08:54 PM
Regular expression does not work in pattern facet? Johann Sijpkes XML 2 07-14-2004 05:30 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM
Serialization of a regular expression pattern MARTIN Herve Java 1 07-22-2003 09:31 PM



Advertisments