python-string parsing-regular-re

Source: Internet
Author: User

Regular expressions

Special character sequences, matching search and replacement text

Normal character + special character + number, normal character used to set boundary

Change character ideas

String functions > Regular > For loop

Meta characters match one character

# metacharacters are capitalized and are generally lowercase

1.0~9 integer \d take counter \d

Import Reexample_str = "Beautiful is better than ugly 78966828 $ \ r \ n ^explicit is better than implicit" print (Re.findal L (r "\d", Example_str)) print (Re.findall (r "\d", Example_str))

2. Letters, numbers, underscores \w reverse \w

Import Reexample_str = "Beautiful is better_ than ugly 78966828 $ \ r \ n ^explicit is better than implicit" print (Re.finda LL (R ' \w ', example_str)) print (Re.findall (R ' \w ', example_str))

3. Blank characters (space, \ t, \ t, \ n) \s reverse \s

Import Reexample_str = "Beautiful is better_ than ugly 78966828 $ \ r \ n ^explicit is better than implicit" print (Re.finda LL (R ' \s ', example_str)) print (Re.findall (R ' \s ', example_str))

4. Any one [] 0-9 A-Z in the character set [^]

Import Reexample_str = "Beautiful is better_ than ugly 78966828 $ \ r \ n ^explicit is better than implicit" print (Re.finda LL (R ' [0-9] ', example_str)) print (Re.findall (R ' [^0-9] ', EXAMPLE_STR))

5. Any character other than \ n

Import Reexample_str = "Beautiful is better_ than ugly 78966828 $ \ r \ n ^explicit is better than implicit" print (Re.finda LL (R ".", Example_str))

The number of words specifies the number of occurrences of the preceding character

1. Greed and non-greed

A. Greedy match by default, maximum match possible until a character does not meet the criteria to stop (maximum match)

B. A non-greedy match, followed by a quantity word? , the minimum satisfying match

C. Greedy and non-greedy use, is a major cause of bugs in the program

Import Reexample_str = "Beautiful is better_ than ugly 78966828 $ \ r \ n ^explicit is better than implicit" print (Re.finda LL (R '. *u ', example_str)) print (Re.findall (R '. *?u ', example_str))

2. Repeat the specified number of times {n} {n, m}

Import Reexample_str = "Beautiful is better_ than ugly 78966828 $ \ r \ n ^explicit is better than implicit" print (Re.finda LL (R ' \d{3} ', Example_str))

3.0 times and infinitely many times *

Import Reexample_str = "Beautiful is better_ than ugly 78966828 $ \ r \ n ^explicit is better than implicit" print (Re.finda LL (R '. * ', EXAMPLE_STR))

4.1 times and infinitely multiple +

Import Reexample_str = "Beautiful is better_ than ugly 78966828 $ \ r \ n ^explicit is better than implicit" print (Re.finda LL (R ' \d+ ', example_str))

5.0 or 1 times? Use idea: go to Heavy

Import Reexample_str = "Beautiful is better_ than ugly 78966828 $ \ r \ n ^explicit is better than implicit" print (Re.finda LL (R ' 7896? ', Example_str))

Boundary matching

1. Match from the beginning of the string ^

2. Match $ from end of string

Regular Expressions or relationships |

Meet | Regular expression on the left or right

Import Reexample_str = "Beautiful is better_ than ugly 78966828 $ \ r \ n ^explicit is better than implicit" print (Re.finda LL (R ' \d+|\w+ ', example_str))

Group

  () the regular expression within the parentheses is treated as a single character, and the contents of the regular match within () are returned, which can be multiple, with relation

python-Regular Correlation Module-re

1. Find the matching regular character from the character FindAll ()

Import rename = "Hello Python 3.7, 123456789" total = Re.findall (r "\d+", name) print (total)

2. Replace the regular match string sub ()

Import Redef Replace (value):    return str (int (value.group ()) + 1) result_str = Re.sub (r "\d", replace, name, 0) print ( RESULT_STR)

Match a Chinese character [\u4e00-\u9fa5]

python-string parsing-regular-re

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.