diff --git a/docs/notes/python-rexp.rst b/docs/notes/python-rexp.rst index 64241d23..2f9b913e 100644 --- a/docs/notes/python-rexp.rst +++ b/docs/notes/python-rexp.rst @@ -95,3 +95,46 @@ Group Comparison '2016-01' >>> m.group(3) '2016' + + +Back Reference +-------------- + +.. code-block:: python + + # compare 'aa', 'bb' + >>> re.search(r'([a-z])\1$','aa') != None + True + >>> re.search(r'([a-z])\1$','bb') != None + True + >>> re.search(r'([a-z])\1$','ab') != None + False + + # compare open tag and close tag + >>> pattern = r'<([^>]+)>[\s\S]*?' + >>> re.search(pattern, ' test ') != None + True + >>> re.search(pattern, '

test

') != None + True + >>> re.search(pattern, ' test ') != None + False + + +Named Grouping ``(?P)`` +----------------------------- + +.. code-block:: python + + # group reference ``(?P...)`` + >>> pattern = '(?P\d{4})-(?P\d{2})-(?P\d{2})' + >>> m = re.search(pattern, '2016-01-01') + >>> m.group('year') + '2016' + >>> m.group('month') + '01' + >>> m.group('day') + '01' + + # back reference ``(?P=name)`` + >>> re.search('^(?P[a-z])(?P=char)','aa') + <_sre.SRE_Match object at 0x10ae0f288>