backreference in regexp

Discussion in 'Python' started by Fredrik Lundh, Jan 31, 2006.

  1. Schüle Daniel wrote:

    > Hello @all,
    >
    > >>> p = re.compile(r"(\d+) = \1 + 0")
    > >>> p.search("123 = 123 + 0")

    >
    > 'search' returns None but I would expect it to
    > find 123 in group(1)
    >
    > Am I using something that is not supported by Python
    > RegExp engine or what is the problem with my regexp?


    plus matches one or more instances of the previous item. to make
    it match a plug sign, you have to escape it:

    p = re.compile(r"(\d+) = \1 \+ 0")

    </F>
     
    Fredrik Lundh, Jan 31, 2006
    #1
    1. Advertising

  2. X-Enigmail-Version: 0.76.5.0
    X-Enigmail-Supports: pgp-inline, pgp-mime
    Content-Type: text/plain; charset=us-ascii; format=flowed
    Content-Transfer-Encoding: 7bit

    Hello @all,

    >>> p = re.compile(r"(\d+) = \1 + 0")
    >>> p.search("123 = 123 + 0")


    'search' returns None but I would expect it to
    find 123 in group(1)

    Am I using something that is not supported by Python
    RegExp engine or what is the problem with my regexp?

    Regards, Daniel
     
    =?ISO-8859-1?Q?Sch=FCle_Daniel?=, Jan 31, 2006
    #2
    1. Advertising

  3. thank you, I completely forgot that + is one of metacharacters

    Regards, Daniel
     
    =?ISO-8859-1?Q?Sch=FCle_Daniel?=, Jan 31, 2006
    #3
    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. paulm

    Newbie backreference question

    paulm, Jun 30, 2005, in forum: Python
    Replies:
    6
    Views:
    386
    paulm
    Jul 1, 2005
  2. Joao Silva
    Replies:
    16
    Views:
    368
    7stud --
    Aug 21, 2009
  3. Bryan Kennerley

    regexp help - substring of a backreference

    Bryan Kennerley, Aug 7, 2010, in forum: Ruby
    Replies:
    4
    Views:
    121
    Bryan Kennerley
    Aug 7, 2010
  4. j.vimal

    Regexp: Negation with backreference?

    j.vimal, May 30, 2006, in forum: Perl Misc
    Replies:
    6
    Views:
    245
    Xicheng Jia
    May 30, 2006
  5. Nisse Engström

    RegExp: Backreference in ClassRange

    Nisse Engström, Jun 22, 2005, in forum: Javascript
    Replies:
    1
    Views:
    91
    Michael Winter
    Jun 22, 2005
Loading...

Share This Page