Regular expressions for Python

Source: Internet
Author: User

1.re.findall ("A", "AB")

A: Match rule ab: what to match

The result is returned as a list: [' a ']

2. Metacharacters: ".", "^", "$", "*", "+", "?", "{}", "[]", "|", "()", "\"

"." : matches any character except the line break "\ n";

Import= Re.findall ('ab.d',"abcdefg"  = Re.findall ('ab.e',"abcdefg") Print # [' ABCD '] Print # []

"^": Match string start

    

Import= Re.findall ('^ab',"abcdefg"  = Re.findall ('^bc',"abcdefg")  Print#[' AB ']print#[]

' $ ': Match string end

Import= Re.findall ('ab$',"abcdefg"  = Re.findall ('fg$',"abcdefg")  Print#[]print#[' FG ']

"*": match the previous character 0 or unlimited times

Import= Re.findall ('ab*',"abbdefg"  = Re.findall ('ab*',"acdefg")  Print#[' ABB ']print#[' a ']

"+": Match the previous character 1 or unlimited times

"?" : matches the previous character 0 or 1 times

' {n} ': matches the previous character N times

' {n,m} ': matches the previous character N to M (closed interval)

"[AB]": Match A or b

ImportReA= Re.findall ('a[bc]d',"ABCDEFG") B= Re.findall ('a[bc]d',"ABDEFG") C= Re.findall ('a[bc]d',"ACDEFG")PrintA#[]PrintB#[' Abd ']PrintC#[' ACD ']

"\": 1. Backslash followed by meta-character, so that metacharacters remove special functions

2. followed by ordinary characters, creating special functions

3. Strings that match the string corresponding to the reference sequence

Regular expressions for Python

Related Article

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.