\1 in regular expression

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • python24
    New Member
    • Nov 2007
    • 13

    \1 in regular expression

    can anyone tell me usage of (\1) in regex??

    Test String: <font size=10><span>l abel<span></font>
    Pattern: <font[^>]*>(.*?)\s*\w+\s *(\1)</font>
    This is working

    but if i change latter <span> to </span>and try to write pattern with (/1),am not getting it.

    Pls give the solution

    Thank you
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by python24
    can anyone tell me usage of (\1) in regex??

    Test String: <font size=10><span>l abel<span></font>
    Pattern: <font[^>]*>(.*?)\s*\w+\s *(\1)</font>
    This is working

    but if i change latter <span> to </span>and try to write pattern with (/1),am not getting it.

    Pls give the solution

    Thank you
    The matched substring will be saved when matched to an expression enclosed in parentheses and can be accessed with the group(group number) method of match objects. The '\1' in your expression matches the text that was matched by group number 1. Since it is in parentheses, it also will be saved. This may give you some ideas:[code=Python]import re

    p = re.compile(r'<f ont[^>]*><?(.*?)>?\s*( \w+)\s*<?/?(\1)>?</font>')

    s = '<font size=10><span>l abel</span></font>'

    m = p.match(s)
    print m.group(0)
    print m.groups()

    >>> <font size=10><span>l abel</span></font>
    ('span', 'label', 'span')
    >>> m.group(1)
    'span'
    >>> m.group(2)
    'label'
    >>> m.group(3)
    'span'
    >>> [/code]Same, but add another group to the expression:[code=Python]p1 = re.compile(r'<f ont\s*([^>]*)><?(.*?)>?\s* (\w+)\s*<?/?(\2)>?</font>')
    s1 = '<font size=10><span>l abel</span></font>'
    m1 = p1.match(s1)
    print m1.group(0)
    print m1.groups()

    '''
    >>> <font size=10><span>l abel</span></font>
    ('size=10', 'span', 'label', 'span')
    >>>
    '''

    s2 = '<font><span>la bel</span></font>'
    m2 = p1.match(s2)
    print m2.group(0)
    print m2.groups()

    '''
    >>> <font><span>lab el</span></font>
    ('', 'span', 'label', 'span')
    >>>
    '''[/code]

    Comment

    • python24
      New Member
      • Nov 2007
      • 13

      #3
      Thank u

      Thank u bv...
      i understood concept but a small doubt..

      in <font[^>]*><?(.*?)>?\s*( \w+)\s*<?/?(\1)>?</font>...y have u used '?' before every expresion?i didnt find differen in its absence...
      Last edited by python24; Jan 11 '08, 06:18 AM. Reason: add more inf

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by python24
        Thank u bv...
        i understood concept but a small doubt..

        in <font[^>]*><?(.*?)>?\s*( \w+)\s*<?/?(\1)>?</font>...y have u used '?' before every expresion?i didnt find differen in its absence...
        The '?' character by itself matches 0 repetitions or 1 repetition of the expression that precedes it. Therefore the match is optional.

        Comment

        Working...