# Coding:utf-8 Import # The python regular expression module text="hi,hiasdf"= Re.findall ("\\bhi "#R is the meaning of raw, indicating that the string is not escaped, if not add R, the output is always empty, because \ will be after the b escaped, if not write R is written \\bprint # The output is [' Hi ', ' Hi ']
Re is the regular expression module in Python. FindAll is one of the methods used to match all eligible strings in the text according to the provided regular expression. The returned result is a list that contains all matches.
"\b" denotes the beginning or end of a word in a regular expression, where spaces, punctuation, and line breaks are the word's segmentation
[] means that any of the characters in parentheses are met. For example, "[Hi]", it is not a match "hi", but match "H" or "I". If you change the regular expression to "[Hh]i", you can match both "HI" and "Hi".
A simplified notation for numbers: [0-9],\d. Similar to the use of [a-za-z].
To represent any number of lengths, you can use [0-9]* or \d*
Note, however, that any length represented by * includes 0, which means that null characters without numbers will be matched. A symbol + similar to *, which represents 1 or longer.
So to match all the number strings, you should use [0-9]+ or \d+
\D{11} matches 11-bit number 1\d{10} matches 11-bit digits starting with 1
Any character is used with "." , while "*" is not a representation of a character, but a quantity: it indicates that the preceding character can be repeated any number of times (including 0 times)
"\s", which represents any character that is not a whitespace symbol
”? "represents any one or 0 characters
Like Hi, I am Shirley Hilton. I am his wife.
Use "i.*e" to match and get [' I am Shirley Hilton. I am his wife '] greedy match--match as long as possible
Match with "I.*?e", get [' I am shirle ', ' I am his wife '] lazy match match as short as possible
From the text below, match all the words that begin with S and end with E.
Site sea Sue Sweet See case SSE Ssee loses
Answer \bs\s*e\b
Attention, not \bs.*?e\b.
Since it's a word, we don't want a space, so we need to use "\s" instead of "."
Python Regular Expressions