Python re.compile (? p<name>)

Source: Internet
Author: User

The regular can also match ... Geeksquiz Web site to provide code questions, can be used to self-test a language mastery situation, today to do Python's topic has an interesting discovery-the original can also write this >>>
Sentence = ' Cats is fast ' regex = Re.compile (? p<animal>\w+) (? p<verb>\w+) (? p<adjective>\w+) ' matched = Re.search (regex, sentence) print (matched.groupdict ())

Output: {' adjective ': ' fast ', ' verb ': ' is ', ' Animal ': ' Cats '}

The instructions in the Python Help documentation are as follows:

The syntax for a named group is one of the python-specific extensions: (?P<name>...) . name is, obviously, the name of the group. Named groups also behave exactly like capturing groups, and additionally associate a name with a group. Thematch object methods that deal with capturing groups all accept either integers this refer to the group by number or St Rings that contain the desired group ' s name. Named groups is still given numbers, so can retrieve information on a group in both ways:

>>> p = re.compile (R ' (? >> m.group (1) ' Lots '

The syntax for backreferences in a expression such as refers to the number of the (...)\1 group. There ' s naturally a variant that uses the group name instead of the number. This is another Python extension: Indicates, the contents of the (?P=name) group calledname should again be Matche D at the current point. The regular expression for finding doubled words, (\b\w+)\s+\1 can also is written as (?P<word>\b\w+)\s+(?P=word) :

>>> p = re.compile (R ' (? p<word>\b\w+) \s+ (? P=word) >>> P.search (' Paris in the Spring '). Group () ' The '

Collation of regular expression documents: https://docs.python.org/2/howto/regex.html

Common

^ Matches The beginning of a line

$ Matches The end of the line

. Matches any character

\s Matches whitespaces

\s Matches any non-whitespace character

* Repeats a character 0 or more times

*? Repeats A character 0 or more times (non-greedy)

+ Repeats a character one or more times

+? Repeats A character one or more times (non-greedy)

( indicates where string extraction is to start

) indicates where string extraction is to end

\d Matches any decimal digit

\d Matches any non-didgit character

\w Matches any alphanumeric character

\w Matches any non-alphanumeric character


Python re.compile (? p<name>)

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.