Python re module

Source: Internet
Author: User

Re modules are primarily used to find, replace, split

Print (re.__doc__)

Special characters (meta-characters):

"." Any one character, except for line breaks (\ n)

"^" String beginning

End of "$" string

"*" >=0 characters, greedy match

"+" >=1 characters, greedy match

"?" 0 or 1 characters, greedy match

{m, n} repeats m to n times characters, greedy match

*?, +?,??, {m, n}? Not greedy, as little as possible

[] One character

"|" Or

() group use, when split, all as a list element

(? p<name>) Character matching aliases

Special sequences:

\a string Start

\z End of string

\b Empty string at start or end position

\b Non-\b

\d [0-9]

\d [^0-9]

\s white space character [\t\n\r\f\v]

\s non-\s [^\s]

\w [a-za-z0-9_] numbers, letters

\w non-numeric, letter

\\   \

Optional function parameters:

Re. I Ignore case

Note: Segmentation does not ignore case

Re. M Multi-line matching

Note: Change the ^$ way: each row is determined

Function:

(1) Find (find one to complete)

Match (Patttern, String)

Match from start of string, no return None

Search (pattern, string)

Match entire string range, no return none

(2) matching conditions

Compile (pattern)

(3) Split

Split (pattern, String)

Match split, return list

(4) Find all

FindAll (pattern, string)

Find all matched substrings, return to list

Finditer (pattern, string)

Find all matched substrings to form an iterator

(5) Replacement

Sub (pattern, REPL, String)

Finds all substrings that match the pattern of the regular expression in string strings, replacing them with another string repl

SUBN (Pattern, REPL, String)

Same sub, return two-dimensional tuple

How to use:

R1 = Re.compile ("[. *]""ABC")
R1 = Re.compile (R"World")
if r1.match ("helloworld"): print(" OK")
Re.split ("(\w+)" "192.168.1.1")

Python re module

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.