Python Sixth Week Study notes (2)

Source: Internet
Author: User

Regular expressions Basic syntax meta-character
Code Description Example
. Match any character except for a line break
[ABC] The character set can only represent one character position. Match any one of the characters contained
[^ABC] The character set can only represent one character position. Matches any character that drops a character within a collection
[A-z] A character range, a collection that represents a character position that matches any one of the characters contained in the
[^a-z] A character range, a collection that represents a character position that matches any character that drops a character within a collection
\b Match the boundaries of a word
\b Do not match the boundaries of a word
\d equals [0-9] matches a digit
\d Equivalent [^0-9] matches a non-numeric
\s Matches 1-bit whitespace characters, including line breaks, tabs, space equivalents [\f\r\n\t\v]
\s Match 1-bit non-whitespace characters
\w Equivalent [a-za-z0-9_] contains Chinese
\w Match characters outside of \w
Escape
    • A symbol with a special meaning in a regular expression, which is escaped using \
Repeat
Code Description Example
* The preceding regular expression repeats 0 or more times
+ The preceding regular expression repeats at least once
The preceding regular expression repeats 0 or 1 times
N Repeat n times
{N,} Repeat more than n times
{N,m} Repeat N to M times
Grouping (capturing) assertions
Code Description Example
X Y Match x or Y
(pattern) Group numbers are automatically assigned after grouping (capture) starting from 1 You can change the priority \ Number match corresponding grouping (refers to the contents of the group on the previous match)
(?:p Attern) Change priority non-grouping only
(? <name>exp) (?name Exp The group captures the naming of the Python syntax as a (? P<NAME>EXP)
(? =exp) 0 width Positive lookahead assertion assertion exp must appear on the right side of the match
(? <=exp) 0 width is recalling the post assertion assertion that exp must appear on the left side of the match
(?! Exp 0 width Negative lookahead assertion asserts that exp must not appear on the right side
(? <!exp) 0 width Negative Review post assertion asserts that Exp must not appear on the left side
(? #comment) Comments

Assertion does not occupy a group number

Greed and non-greed
    • Default greedy mode, as many matching strings as possible
Code Description Example
*? Match any number of times and repeat as little as possible
+? Match at least once and repeat as little as possible
?? Match 0 or 1 times and repeat as little as possible
{n}? Match at least n times and repeat as little as possible
{n,m}? Match at least N times, up to M times, as few repetitions as possible
Engine options
Code Description Example
IgnoreCase Ignore case when matching Re. Ire.ignorecase
Singleline Single-line mode, can penetrate/n Re. Sre.dotall
Multiline Multi-line mode Re. Mre.multiline
Ignorepatternwhitespace Ignore expression whitespace character, to use white space characters Re. Xre.verbose

Code Description Example
IgnoreCase match ignores case re. Ire.ignorecase
Singleline single-line mode, which penetrates the/n re. Sre.dotall
Multiline Multi-line mode re. Mre.multiline
Ignorepatternwhitespace ignores the expression whitespace characters to use the white-space character re. Xre.verbose

Use | (bitwise OR) operations in Python to turn on multiple options

Python Regular Expressions RE module compilation
    • Re.compile (pattern, flags=0)
      • Returns the regular Expression object regex
      • The results of the regular expression compilation are saved, and the next time you use the same pattern, you do not need to recompile
One-time match
    • Regex.match (string[, pos[, Endpos])

      • Match from beginning of string to specify start and end position return match object
    • Regex.search (string[, pos[, Endpos])

      • Search from scratch until the first match, specifying the start and end positions to return the match object
    • Regex.fullmatch (string[, pos[, Endpos])
      • Match the entire string to the regular expression
Full-Text Search
    • Regex.findall (string[, pos[, Endpos])

      • Matches the entire string from left to right, returning a list of all occurrences
    • Regex.finditer (string[, pos[, Endpos])
      • Matches the entire string, left-to-right, and returns all occurrences of the iterator, each of which is a match object
Match substitution
    • Regex.sub (Replacement, string, count=0)

      • Use pattern to match string strings and replace matches with replacement
      • Replacement can be string, bytes, function
    • Regex.subn (Replacement, string, count=0)
      • Functions like a sub return a tuple (new_string, Number_of_subs_made)
Split string
    • Regex.Split (String, maxsplit=0)
      • Return to List
Group
    • using (pattern) captured data to put in a group

    • Match class method
    • Group (N)

      • 1-n the corresponding group 0 but will match the entire string
      • If a named grouping is used, the group (name) method can be taken out
    • Groups ()

      • Returns a tuple of all groups
    • Groupdict ()
      • Returns a dictionary of all named groupings
Todo:argparse

Python Sixth Week Study notes (2)

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.