The RE module in Python provides regular expression-related operations.
Character:
. Match any character other than line break
\w match letters or numbers or underscores or kanji
\s matches any whitespace character
\d Matching numbers
\b Match the beginning or end of a word
^ Start of matching string
$ match End of string
Number:
* Repeat 0 or more times
+ Repeat one or more times
? Repeat 0 or one time
{n} repeats n times
{n,} repeats n or more times
{N,m} repeats n to M times
Focus Re.match () match from scratch, match successfully returns an object, unmatched success returns none
Re.search () Browse all strings to match the first rule-compliant string
Focus Re.findall () Place all matching content in one list
Re.sub ()
Re.splite ()
Match ()
1. No grouping
1 ImportRe2Origin ="Hello alex fuck hehe nn"3R = Re.match ("h\w+", origin)4 Print(R.group ())#get all the results that match to5 Print(R.groups ())#gets the grouped results that are matched in the model6 Print(R.groupdict ())#gets the grouped results matched to in the model, the group that executed the key
2. There are groups
1 ImportRe2Origin ="Hello alex fuck hehe nn"3R = Re.match ("(? P<N1>H) (? p<n2>\w+)", origin)4 Print(R.group ())#get all the results that match to5 Print(R.groups ())#gets the grouped results that are matched in the model6 Print(R.groupdict ())#gets the grouped results matched to in the model, the group that executed the key
Re
"Hello Alex fuck hehe nn"
R = Re.match ("h\w+", origin)
Print #获取匹配到的所有结果
Print (R.groups ()) #获取模型中匹配到的分组结果
Print (R.groupdict ()) #获取模型中匹配到的分组结果, the group that executed the key
python-Regular Expression 1