Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Regexp problem

Reply
Thread Tools

Regexp problem

 
 
Beldar
Guest
Posts: n/a
 
      07-30-2009
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!
 
Reply With Quote
 
 
 
 
Tim Chase
Guest
Posts: n/a
 
      07-30-2009
> 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:

>>> import re
>>> s = "lalala lalala tiruri beldar-is-listening tiruri lalala"
>>> r = re.compile(r'(\w+)-is-listening')
>>> r.search(s).group(1)

'beldar'
>>> r.sub('this is a replacement', s)

'lalala lalala tiruri this is a replacement tiruri lalala'

-tkc


 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      07-30-2009
Beldar wrote:
> 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())
 
Reply With Quote
 
Beldar
Guest
Posts: n/a
 
      07-30-2009
On 30 jul, 15:07, MRAB <(E-Mail Removed)> wrote:
> Beldar wrote:
> > 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())


Ok, thank you all, it was very helpful!
 
Reply With Quote
 
Peter Brett
Guest
Posts: n/a
 
      07-30-2009
Marcus Wanner <(E-Mail Removed)> writes:

> On 7/30/2009 9:32 AM, Beldar wrote:
>> On 30 jul, 15:07, MRAB <(E-Mail Removed)> wrote:
>>> Beldar wrote:
>>>> 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())

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

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


I have to confess that after fiddling with regexps for quite a while
with no great success, I learnt the hard (and best) way, i.e. using
them to write something vile and horrible.[*] I commend this path to
you also.

Cheers,

Peter
[*] http://git.gpleda.org/?p=gaf.git;a=b...8n;h=6fab9b85b

--
Peter Brett <(E-Mail Removed)>
Remote Sensing Research Group
Surrey Space Centre
 
Reply With Quote
 
Ethan Furman
Guest
Posts: n/a
 
      07-30-2009
Marcus Wanner wrote:
> On 7/30/2009 9:32 AM, Beldar wrote:
>
>> On 30 jul, 15:07, MRAB <(E-Mail Removed)> wrote:
>>
>>> Beldar wrote:
>>>
>>>> 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())

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

>
> 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~
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      07-30-2009
Ethan Furman wrote:
> Marcus Wanner wrote:
>> On 7/30/2009 9:32 AM, Beldar wrote:
>>
>>> On 30 jul, 15:07, MRAB <(E-Mail Removed)> wrote:
>>>
>>>> Beldar wrote:
>>>>
>>>>> 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())
>>>
>>>
>>> Ok, thank you all, it was very helpful!

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

+1

I have the first edition, seventh printing (December 199. 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.
 
Reply With Quote
 
Ethan Furman
Guest
Posts: n/a
 
      07-31-2009
MRAB wrote:
> Ethan Furman wrote:
>
>> Marcus Wanner wrote:
>>> 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!
>>

> +1
>
> I have the first edition, seventh printing (December 199. 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~
 
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
new RegExp().test() or just RegExp().test() Matěj Cepl Javascript 3 11-24-2009 02:41 PM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
Programmatically turning a Regexp into an anchored Regexp Greg Hurrell Ruby 4 02-14-2007 06:56 PM
RegExp.exec() returns null when there is a match - a JavaScript RegExp bug? Uldis Bojars Javascript 2 12-17-2006 09:59 PM



Advertisments