Python Learning section 14th (Regular)

Source: Internet
Author: User

Both Python2 and Python3 have two types of strings
Str
bytes

Re module
The Find-class functions are exact lookups.
String is a fuzzy match

FindAll (Pattern,string,flags)

Replace function
' Hello python '. replace (' P ', ' P ')
' Hello Python '

A= ' sadfadf232wwewfr323rwef34534trwef '
Import re
W=re.findall (' \d ', ' sadfadf232wwewfr323rwef34534trwef ')
W=re.findall (' \d+ ', ' sadfadf232wwewfr323rwef34534trwef ')
Print (W)

Re.findall the results of all conforming rules to a list.
Re.findall (pattern,string)
Key: Meta-character (character with special function)
Wildcard characters:
. A point can match any symbol except a newline character, which means that the point can represent any symbol.
* [0,+00] 0 to infinity Range
+ [1,+00] 1 to infinity Range
? [0,1] 0 or 1 x
{} {N,m} can write {3,5} 3 to 5 can also write {3} 3, you can also write {0,} infinite times meaning

Character:
[] Usage, re.findall ("A[bd]c", "SAFASDFAEFSDF") inside [BD] is or is the relationship, or B or D are displayed
If the punctuation is added to the [], the punctuation and the letter are the same as the content of a, B, and so on.
[] If you add a wildcard, then there is no wildcard meaning, just match the content of the relevant symbol.
[0-9] Just match a number, if you need to find 4 digits, [0-9]{4}
[] characters with special meanings in the character set are:-^ \

^ Start Matching
$ End Match

(): Group

(AD) matches this group
(AD) in the +yuan graph below this match is blue content, but grouping first displays () the contents within the group, so the result is the ad

As shown, you can cancel () the priority, print the blue content,?: This syntax is to cancel the priority, not only to display the grouping, but also to display other content.

For example, the printed content is 3, because there are many numbers, matching to yuan, but displaying grouped content 3

Ungroup Attributes

The meaning of a.
Empty if or after no match is shown.

For example, the above can directly match the URL.

Translate, translate letters into special meanings, and transfer characters of special meaning to ordinary characters
\d represents any one character of 0-9
\w a character that represents any number or letter

\. Point becomes normal symbol

A bit more complicated.

\s out, there's a space.

Search method
Find a corresponding one that no longer matches backwards

Match method
Matches only the beginning, or none if not at the beginning

Homework

Group names to groups, pick content by name

Special symbols that can be used in a character set

| pipe character

Python Learning section 14th (Regular)

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.