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>)