Regexp problem

Discussion in 'Python' started by Beldar, Jul 30, 2009.

  1. Beldar

    Beldar Guest

    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!
     
    Beldar, Jul 30, 2009
    #1
    1. Advertising

  2. Beldar

    Tim Chase Guest

    > 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
     
    Tim Chase, Jul 30, 2009
    #2
    1. Advertising

  3. Beldar

    MRAB Guest

    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())
     
    MRAB, Jul 30, 2009
    #3
  4. Beldar

    Beldar Guest

    On 30 jul, 15:07, MRAB <> 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!
     
    Beldar, Jul 30, 2009
    #4
  5. Beldar

    Peter Brett Guest

    Marcus Wanner <> writes:

    > On 7/30/2009 9:32 AM, Beldar wrote:
    >> On 30 jul, 15:07, MRAB <> 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=blob;f=libgeda/desktop-i18n;h=6fab9b85b

    --
    Peter Brett <>
    Remote Sensing Research Group
    Surrey Space Centre
     
    Peter Brett, Jul 30, 2009
    #5
  6. Beldar

    Ethan Furman Guest

    Marcus Wanner wrote:
    > On 7/30/2009 9:32 AM, Beldar wrote:
    >
    >> On 30 jul, 15:07, MRAB <> 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~
     
    Ethan Furman, Jul 30, 2009
    #6
  7. Beldar

    MRAB Guest

    Ethan Furman wrote:
    > Marcus Wanner wrote:
    >> On 7/30/2009 9:32 AM, Beldar wrote:
    >>
    >>> On 30 jul, 15:07, MRAB <> 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 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. :)
     
    MRAB, Jul 30, 2009
    #7
  8. Beldar

    Ethan Furman Guest

    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 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~
     
    Ethan Furman, Jul 31, 2009
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg Hurrell
    Replies:
    4
    Views:
    164
    James Edward Gray II
    Feb 14, 2007
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    496
    Mikel Lindsaar
    Mar 31, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    368
    7stud --
    Aug 21, 2009
  4. Uldis  Bojars
    Replies:
    2
    Views:
    194
    Janwillem Borleffs
    Dec 17, 2006
  5. Matìj Cepl

    new RegExp().test() or just RegExp().test()

    Matìj Cepl, Nov 24, 2009, in forum: Javascript
    Replies:
    3
    Views:
    185
    Matěj Cepl
    Nov 24, 2009
Loading...

Share This Page