Python Regular summary

Source: Internet
Author: User

Re Summary qualifier (control repeat count)

?Matches the preceding subexpression 0 or one time. For example, do (es) can match "do" in "Do", "does" in "does", "Doxy" in "Do"? Equivalent to {0,1}.
*Matches the preceding subexpression 0 or more times. For example, zo* can match "z" and "Zoo". * Equivalent to {0,}.
+Matches the preceding subexpression one or more times. For example, ' zo+ ' can match "Zo" and "Zoo", but not "Z". + equivalent to {1,}.
{n}N is a non-negative integer. Matches the determined n times. For example, ' o{2} ' cannot match ' o ' in ' Bob ', but can match two o in ' food '.
{n,}N is a non-negative integer. Match at least n times. For example, ' o{2,} ' cannot match ' o ' in ' Bob ', but can match all o in ' Foooood '. ' O{1,} ' is equivalent to ' o+ '. ' O{0,} ' is equivalent to ' o* '.
{n,m}Both M and n are non-negative integers, where n <= m. Matches at least n times and matches up to M times. For example, "o{1,3}" will match the first three o in "Fooooood". ' o{0,1} ' is equivalent to ' O? '. Note that there can be no spaces between a comma and two numbers.

Locator characters

^Matches the starting position of the input string. If you set the Multiline property of the RegExp object, ^ will also match the position after \ n or \ r.
$Matches the position of the end of the input string. If you set the Multiline property of the RegExp object, the $ will also match the position before \ n or \ r.
\bMatches a word boundary, which is the position between the word and the space.
\BNon-word boundary match.

Character classification

\dAny number from 0 to 9
\DAny character except a number from 0 to 9
\wAny letter, number, or underscore character (which can be considered a matching "word" character)
\WAny character except letters, numbers, and underscores
\sSpaces, tabs, or newline characters (which can be thought of as matching "whitespace")
\SAny character except spaces, tabs, and newline characters

Their own character classification

[]
[abc]Matches any character within the square brackets (such as a, B, or C).
[^abc]Matches any character that is not in square brackets.

Use search and match in Python
import reprint("# 检验是否为大小写"='adkkdak'='abc123efg'= re.search('^[a-z]+$', s1)if lowerRe:    print"全为小写")else:    print"不全为小写"= re.match('[a-z]+$', s2)if lowerRe:    print"全为小写")else:    print"不全为小写")

The reason

    1. Regular expressions are not part of Python and need to refer to the RE module when used

    2. The matching form is: Re.search (regular expression, with matching string) or re.match (regular expression, with matching string). The difference is that the latter begins with the start character (^) by default. Sore.search(‘^[a-z]+$‘, s1) 等价于 re.match(‘[a-z]+$‘, s2)

    3. If the match fails, an = re.search(‘^[a-z]+$‘, s1) none is returned

Compile and FindAll
print("# 匹配数字"="12aa123bb1234cc12345dd"== re.compile('\d+')forin testRe.findall(s1):    s.append(i)print("s1 = ", s1)print(" ".join(s))
    1. Compile is to create an expression object, which is a good way to call

    2. FindAll actually returns a list, so use for i in nothing wrong

    3. Group A group () will be in the general brackets.
      Example:

      import="123abc456"print re.search("([0-9]*)([a-z]*)([0-9]*)",a).group(0)   #123abc456,返回整体print re.search("([0-9]*)([a-z]*)([0-9]*)",a).group(1)   #123print re.search("([0-9]*)([a-z]*)([0-9]*)",a).group(2)   #abcprint re.search("([0-9]*)([a-z]*)([0-9]*)",a).group(3)   #456

Python Regular summary

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.