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