The most commonly used regular expression symbols in Python


Insert image description here


symbol meaning example Match results
* Matches the preceding character, subexpression, or bracketed character 0 or more times a * b * aaaaaaaa, aaabbbbb,bbbbbb
+ Matches the preceding character, subexpression, or bracketed character at least 1 time a+b+ aaaaaaab, aaabbbbb,abbbbb
[ ] Match any character in the square brackets (equivalent to "pick any one") [A-Z]* APPLE, CAPITALS,QWERTY
( ) Expression grouping (grouping will take precedence in regular expression rules) (a * b) * Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
{m,n} Matches the preceding character, subexpression, or bracketed character m to n times (inclusive) a{2,3}b{2,3} aabbb, aaabbb, aabb
[^] Matches any character not enclosed in square brackets [^A-Z]* apple, lowercase,qwerty
I Matches any character or subexpression separated by a vertical bar (note that it is a vertical bar, not the capital letter I) b(aIiIe)d bad, bid, bed
. Match any single character (including symbols, numbers, spaces, etc.) b.d bad, bzd, b$d, b d
^ A character or subexpression that refers to the beginning of a string ^a apple, asdf, a
\ Escape characters (convert characters with special meaning into literal form) \ .\I\ .I\
$ Often used at the end of a regular expression to mean "match from the end of the string". Without it, each regular expression actually carries the ".*" pattern and will only match from the beginning of the string. This symbol can be seen as the antonym of the ^ symbol [A-Z][a-z]$ ABCabc, zzzyx, Bob
?! "Not included". This strange combination is usually placed in front of a character or regular expression, indicating that the character cannot appear in the target string. This notation is difficult to use because characters usually appear in different parts of the string. If you want to completely exclude a character from the entire string, add the ^ and $ symbols ^((?![A-Z]).)*$ no-caps-here, $ymb0lsa4e f!ne

Insert image description here


reference

  1. Book - The Definitive Guide to Python Web Crawling (2nd Edition)

Guess you like

Origin blog.csdn.net/wwt18811707971/article/details/126904364