Python Re Module introduction _python

Source: Internet
Author: User

Python relay literal characters

A regular expression uses a backslash "\" to represent a particular form or as an escape character. This is in conflict with Python syntax, so python uses "\\\\" to represent "\" in the regular expression, because if you want to match "\" in the regular expression, you need to escape with \ to "\". The Python syntax also needs to be escaped from each of the strings, so it becomes "\\\\."
Is it a hassle to write this, and in order for the regular expression to be more readable, python specifically designed the raw string (raw string) to remind you not to use raw string when writing a file path, and there is a trap here. A raw string is a prefix of "R" as a string, such as r "\ n": two characters "\" and "n", not line breaks. This form is recommended when writing regular expressions in Python.

Regular expression meta-character description

.  Matches any character other than newline characters
^ start of  matching string The
end of the match  string
[] used to match a specified character category
?  Repeat 0 times to 1 times for the preceding character
* Repeat  0 times to Infinity for previous characters
{} Repeat M times
{m,n} for previous character repeat to M to n
\d match number, equivalent to [0-9]
\d matches any non-numeric character, equivalent to [^0-9]
\s matches any whitespace, equivalent to [FV]
\s matches any non-white-space character, equivalent to [^ FV]
\w matches any alphanumeric character, equivalent to [a-za-z0-9_ ]
\w matches any non-alphanumeric character, equivalent to [^a-za-z0-9_]
\b matches the beginning or end of a word

Module function description is an example
Re.compile compiles a regular expression into a pattern object

Compile (pattern, flags=0)

First parameter: rule
Second parameter: Flag bit

Re.match matches only the start of a string, if the string does not start with a regular expression, the match fails and the function returns none

Match (pattern, string, flags=0)

First parameter: rule
Second parameter: Represents the string to match
Third parameter: Peugeot bit, used to control how regular expressions are matched

Re.search matches the entire string until a match is found

Search (pattern, string, flags=0)

First parameter: rule
Second parameter: Represents the string to match
Third parameter: Peugeot bit, used to control how regular expressions are matched

>>> Import re
>>> pattern = re.compile (R ' Linuxeye ')
>>> match = Pattern.match (' Jb51.net ')
>>> print match
<_sre. Sre_match object at 0x7f4e96e61c60>
>>> print match.group ()
linuxeye
>>> m = Pattern.match (' blog.jb51.net ') #match匹配开头, not found
>>> print m
None
>>> m = pattern.search (' blog.jb51.net ') #search匹配整个字符串 until a match
>>> print m <_sre is found
. Sre_match object at 0x7f15abfc6b28>
>>> print m.group ()
Linuxeye
>>> m = Re.match (R ' Linuxeye ', ' jb51.net ') #不用re. Compile
>>> print m
<_sre. Sre_match object at 0x7f4e96e61b90>
>>> print m.group ()
linuxeye
>>> m = Re.match ( R ' Linuxeye ', ' www.jb51.net ')
>>> print M
None

Re.split used to split strings

Split (pattern, string, maxsplit=0)

First parameter: rule
Second argument: string
The third parameter: the maximum split string, the default is 0, which means that each match is split
Instance: splitting all strings

>>> import re
>>> test_str = "1 2 3 4 5"
>>> re.split (R ' \s+ ', test_str)
[' 1 ', ' 2 ', '  3 ', ' 4 ', ' 5 ']
>>> re.split (R ' \s+ ', test_str,2) #分割前2个
[' 1 ', ' 2 ', ' 3 4 5 ']
 
>>> test_str = "1. 2.3.4. 5 "
>>> re.split (R ' \.", test_str)
[' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']
>>> re.split (R '. ', Test_str, 3)
[' 1 ', ' 2 ', ' 3 ', ' 4.5 ']

Re.findall the string that matches the rule in the destination string

FindAll (Pattern, string, flags=0)

First parameter: rule
Second argument: Destination string
But three parameters: You can also select items with a rule later
The result returned is a list that is stored in a rule-compliant string and returns a null value if there is no rule-compliant string to find.

>>> import re
>>> test_mail = ' <test01@gmail.com> <test02@gmail.org> Test03@gmail.net '
>>> mail_re = re.compile (R ' \w+@g....\.[ A-Z]{3} ')
>>> re.findall (mail_re,test_mail)
[' test01@gmail.com ', ' test02@gmail.org ', ' Test03@gmail.net ']

Re.sub replacement work based on regular expressions

Sub (pattern, REPL, String, count=0)

First parameter: rule
Second argument: replaced string
Third argument: string
Fourth parameter: Number of replacements. The default is 0, which means that each match is replaced

>>> test = ' blog.jb51.net jb51.net '
>>> test_re = re.compile (R ' \. ')
>>> re.sub (Test_re, '--', test ')
' blog--linuxeye--com linuxeye--com '
>>> re.sub (Test_re, '--', test,1 ',
' blog--jb51.net jb51.net '

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.