diff --git a/docs/source/notes/python-rexp.rst b/docs/source/notes/python-rexp.rst index c0ad72b1..64241d23 100644 --- a/docs/source/notes/python-rexp.rst +++ b/docs/source/notes/python-rexp.rst @@ -2,6 +2,67 @@ Python Regular Expression cheatsheet ==================================== +Compare HTML tags +----------------- + ++------------+--------------+--------------+ +| tag type | format | example | ++============+==============+==============+ +| all tag | <[^>]+> |
, | ++------------+--------------+--------------+ +| open tag | <[^/>][^>]*> | , | ++------------+--------------+--------------+ +| close tag | ]+> |

, | ++------------+--------------+--------------+ +| self close | <[^/>]+/> |
| ++------------+--------------+--------------+ + + +.. code-block:: python + + # open tag + >>> re.search('<[^/>][^>]*>', '
') != None + True + >>> re.search('<[^/>][^>]*>', '') != None + True + >>> re.search('<[^/>][^>]*>', '') != None + True + >>> re.search('<[^/>][^>]*>', '
') != None + False + + # close tag + >>> re.search(']+>', '') != None + True + + # self close + >>> re.search('<[^/>]+/>', '
') != None + True + +``re.findall()`` match string +----------------------------- + +.. code-block:: python + + # split all string + >>> re.findall('[\w]+', source) + ['Hello', 'World', 'Ker', 'HAHA'] + + # parsing python.org website + >>> import urllib + >>> import re + >>> s = urllib.urlopen('https://www.python.org') + >>> html = s.read() + >>> s.close() + >>> print "open tags" + open tags + >>> re.findall('<[^/>][^>]*>', html)[0:2] + ['', '