Pattern Search Regular Expression

S

subhabangalore

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
 
S

Steven D'Aprano

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?
 
M

Mark Lawrence

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
 
D

Denis McMahon

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.
 
M

Mark Lawrence

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
 
R

rusi

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]
 
D

Denis McMahon

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() for i in (0, -1)]
middle = sentence.split()[1:-2]
 
D

Denis McMahon

first_and_last = [sentence.split() 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.
 
M

Mark Lawrence

first_and_last = [sentence.split() 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
 
S

subhabangalore

first_and_last = [sentence.split() 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.
 
A

Andreas Perstinger

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

http://mattgemmell.com/2008/12/08/what-have-you-tried/

Just spell out what you want:
A word at the beginning, followed by any text, followed by a word at
the end.
Now look up the basic regex metacharacters and try to come up with a
solution (Hint: you will need groups)

http://docs.python.org/3/howto/regex.html#regex-howto
http://docs.python.org/3/library/re.html#regular-expression-syntax

Bye, Andreas
 
M

Mark Lawrence

S

subhabangalore

Start here http://docs.python.org/2/library/re.html



Would you also please read and action this,

http://wiki.python.org/moin/GoogleGroupsPython , thanks.



--

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

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,
......

Regards,
Subhabrata.
 
M

Mark Lawrence

On 15/06/2013 17:28, (e-mail address removed) wrote:

You've been pointed at several links, so what have you tried, and what,
if anything, went wrong? Or do you simply not understand, in which case
please say so and we'll help. I'm not trying to be awkward, it's simply
known that you learn more if you try something yourself, rather than be
spoon fed it.

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

rurpy

On 06/15/2013 03:42 AM, (e-mail address 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.

Since nobody here seems to want to answer your question
(or seems even able to read it), I'll try. Is something
like this what you want?

import re

texts = [
'(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"']

pattern = re.compile (r'^\((.*)\)\s*"\S+\s*(.*)\s\S+"$')
for txt in texts:
matchobj = re.search (pattern, txt)
number, midtext = matchobj.group (1, 2)
print ("(%s) %s" % (number, midtext))
 
S

subhabangalore

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

Dear Group,

Thank you for the answer. But I want to learn bit of interesting regular expression forms where may I? No Mark, thank you for your links but they were not sufficient. I am looking for more intriguing exercises, esp use of or in the pattern search.

Regards,
Subhabrata.
 
R

rurpy

Thank you for the answer. But I want to learn bit of interesting
regular expression forms where may I?
No Mark, thank you for your links but they were not sufficient.

Links to the Python reference documentation are useful for people
just beginning with some aspect of Python; they are for people who
already know Python and want to look up details. So it's no
surprise that you did not find them useful.
I am looking for more intriguing exercises, esp use of or in
the pattern search.

Have you tried searching on Google for "regular expression tutorial"?
It gives a lot of results. I've never tried any of them so I can't
recommend any one specifically but maybe you can find something
useful there?

There is also a Python Howto on regular expressions at
http://docs.python.org/3/howto/regex.html

Also, maybe the book "Regular Expressions Cookbook" would
be useful? It seems to have a lot of specific expressions
for accomplishing various tasks and seems to be online for
free at
http://it-ebooks.info/read/920/
 
S

subhabangalore

Links to the Python reference documentation are useful for people

just beginning with some aspect of Python; they are for people who

already know Python and want to look up details. So it's no

surprise that you did not find them useful.







Have you tried searching on Google for "regular expression tutorial"?

It gives a lot of results. I've never tried any of them so I can't

recommend any one specifically but maybe you can find something

useful there?



There is also a Python Howto on regular expressions at

http://docs.python.org/3/howto/regex.html



Also, maybe the book "Regular Expressions Cookbook" would

be useful? It seems to have a lot of specific expressions

for accomplishing various tasks and seems to be online for

free at

http://it-ebooks.info/read/920/

Dear Group,

Thank you for the links. Yes, HOW-TO is good. The cook book should be good.Internet changes its contents so fast few days back there was a very good Regular Expression Tutorial by Alan Gauld or there were some mail discussions, I don't know where they are gone. There is one Gauld's tutorial but I think I read some think different.

Regards,
Subhabrata.
 
T

Terry Reedy

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.
 
R

rurpy

Oops...

Links to the Python reference documentation are useful for people
just beginning with some aspect of Python; they are for people who
already know Python and want to look up details.

That was supposed to be:
Links to the Python reference documentation are NOT useful for people
just beginning with some aspect of Python

and as long as I'm revising, I mean that as a general statement,
nothing wrong with a reference doc link accompanying a simpler
explanation or pointer thereto.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top