Python Learning 4 Regular expressions

Source: Internet
Author: User
Tags alphabetic character

Symbol Description Example
Literal Match the value of a string Foo
Re1|re2 Match regular expression Re1 or Re2 Foo|bar
. Match any character (except line breaks) B.b
^ Match the start of a string ^dear
$ Match the end of a string /bin/*sh$
* Matches the preceding occurrence of the regular expression 0 or more times [a-za-z0-9]*
+ Matches the preceding occurrence of the regular expression 1 or more times [a-z]+\.com
? Matches the preceding occurrence of the regular expression 0 or more times Goo?
N Matches the preceding occurrence of the regular expression n times [0-9] {3}
{M,n} Matches regular expressions that recur m-n times [0-9] {5,9}
[...] Match any one of the characters that appear in the character set [Aeiou]
[.. X-y:] Match any character from character X to Y [0-9],[a-za-z]
[^...] does not match any one of the characters that appear in this character set,
Include a range of characters
[^a-za-z0-9_]
(*|+|?| {}) For any "non-greedy" version that appears above
Repeat match number symbol (*,+,?,{})
.*? [A-z]
(...) Match enclosing parentheses in a regular expression (RE), and keep it as a child group ([0-9]{3})?, f (oo|u) bar
Special symbols
\d Match any number, and [0-9]
(\d is the inverse of \d: any non-number symbol)
Data\d+.txt
\w Match any numeric alphabetic character
(\w is the \w)
[a-za-z_]\w+
\s Matches any whitespace character, and [\n\t\r\v\f]
(\s is the \s)
Of\sthe
\b Match word boundaries
(\b is the \b)
\bthe\b
\nn Match a saved child group Price: \16
\c Match the special character C individually \.,\\,\*
\a (\z) Start of Match string (end) \adear
Common regular expression functions and methods
Compile (pattern,flags=0) Compiles the regular expression pattern and returns a Regex object
Match (Pattern,string,flags=0) Match the string with the regular expression pattern,
Match successfully returns the Match object, otherwise none is returned
Search (pattern,string,flags=0) Matches the first occurrence of a string with a regular expression pattern,
Match successfully returns the Match object, otherwise none is returned
FindAll (Pattern,string[,flags]) Finds all (non-repeating) occurrences of the regular expression pattern in string strings,
Returns a list of matching objects
Finditer (Pattern,string[,flags]) Searches for a string, returning an iterator that sequentially accesses each match result (Match object)
Split (pattern,string,max=0) Returns a list after the string is split in pattern mode.
Max is used to specify the maximum number of splits and does not specify that all will be split.
Sub (pattern,rep1,string,max=0) Returns the replaced string after using REPL to replace each substring of a string that matches the pattern.
Group (num=0) Return all matching objects
Groups () Returns a tuple that contains all of the matched child tuples

Examples of greedy and non-greedy matching:

1 ImportRe2 3Data='Thu Nov 01:14:11 2001::[email protected]::6777874277-5-8'4 5 6 #non-greedy match7patt4='.+? (\d+-\d+-\d+)'8m=Re.match (PATT4, data)9 Print(M.group (1))Ten  One #Greedy Match A  -patt5='. + (\d+-\d+-\d+)' -m=Re.match (PATT5, data) the Print(M.group (1))

Output Result:

6777874277-5-87-5-8

Python Learning 4 Regular expressions

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.