python-Regular Expression 1

Source: Internet
Author: User

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

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.