Regexp problem

B

Beldar

Hi there!

I have a problem and i'm not very good at regular expressions.
I have a text like "lalala lalala tiruri beldar-is-listening tiruri
lalala" I need a regexp to get the 'beldar' part, the format is
'something-is-listening', i need to get the something part, use it in
my code, and then replace the whole 'something-is-listening' for
another string.

Someone can help me please? Thank you!
 
T

Tim Chase

I have a problem and i'm not very good at regular expressions.
I have a text like "lalala lalala tiruri beldar-is-listening tiruri
lalala" I need a regexp to get the 'beldar' part, the format is
'something-is-listening', i need to get the something part, use it in
my code, and then replace the whole 'something-is-listening' for
another string.


Pretty easy:
'lalala lalala tiruri this is a replacement tiruri lalala'

-tkc
 
M

MRAB

Beldar said:
Hi there!

I have a problem and i'm not very good at regular expressions.
I have a text like "lalala lalala tiruri beldar-is-listening tiruri
lalala" I need a regexp to get the 'beldar' part, the format is
'something-is-listening', i need to get the something part, use it in
my code, and then replace the whole 'something-is-listening' for
another string.
\w+ will match a word and enclosing it in (...) will capture what was
matched:

m = re.search(r"(\w+)-is-listening", text)
print "Captured '%s'" % m.group(1)
print "Matched from %d to %d" % (m.start(), m.end())
 
B

Beldar

\w+ will match a word and enclosing it in (...) will capture what was
matched:

     m = re.search(r"(\w+)-is-listening", text)
     print "Captured '%s'" % m.group(1)
     print "Matched from %d to %d" % (m.start(), m.end())

Ok, thank you all, it was very helpful!
 
E

Ethan Furman

Marcus said:
Wow, I really need to learn more about regexp...
Any tutorials you guys can recommend?

Marcus

Mastering Regular Expressions
Powerful Techniques for Perl and Other Tools
By Jeffrey E. F. Friedl

Great book!

~Ethan~
 
M

MRAB

Ethan said:
Mastering Regular Expressions
Powerful Techniques for Perl and Other Tools
By Jeffrey E. F. Friedl

Great book!
+1

I have the first edition, seventh printing (December 1998). It refers to
the 'regex' module of Python 1.4b1, which was subsequently replaced by
the current 're' module and then removed from the standard library. I
hope it's been updated since then. :)
 
E

Ethan Furman

MRAB said:
+1

I have the first edition, seventh printing (December 1998). It refers to
the 'regex' module of Python 1.4b1, which was subsequently replaced by
the current 're' module and then removed from the standard library. I
hope it's been updated since then. :)

I have the second edition (no idea which printing ;), and according to
his preface it has indeed been much updated. Most examples are in perl,
the few in python are decent. The knowledge embodied seems very
thorough. Since I've had the book (two weeks now?) I've been able to
solve two otherwise thorny issues using regular expressions. Yay!

~Ethan~
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top