3.2.5.1 check cards with one hand and 3.2.5.1 cards with one hand
Now let's start learning the regular expressions one by one and use them in a comprehensive manner. In this example, the regular expression is used to check whether the five playing cards are valid. To make the display look better, first write an output function:
Def displaymatch (match ):
If match is None:
Return print (None)
Return print ('<Match: % r, groups = % r>' % (match. group (), match. groups () assumes that each card is composed of five cards. Use a to represent the trump card, k to represent the king, q to represent the queen, and j to represent jack, t indicates 10, 2-9 indicates the corresponding number card. In the following example, check whether the card is valid:
Valid = re. compile (r "^ [a2-9tjqk] {5} $ ")
Displaymatch (valid. match ("akt5q") # Valid
Displaymatch (valid. match ("akt5e") # Invalid.
Displaymatch (valid. match ("Ach") # Invalid.
Displaymatch (valid. match ("727ak") # Valid.
The output is as follows:
Check for a poker card
<Match: 'akt5q', groups = ()>
None
None
<Match: '727ak', groups = ()>
In this example, e does not exist, which leads to an illegal situation. However, there are only three pieces of Hines and less than five are not legal.
Regular Expression: r "^ [a2-9tjqk] {5} $", where ^ indicates matching only from the string; [] indicates matching any character in brackets; a2-9tjqk indicates Character Set combination, 2-9 indicates all numbers in order of 2 to 9; {5} indicates matching at least 5 Characters for success; $ indicates matching before the end of the word.
In the last hand card 727ak, there will be two identical cards, so how can we find the same card, as shown below:
Pair = re. compile (r ". * (.). * \ 1 ")
Displaymatch (pair. match ("717ak") # Pair of 7 s.
Displaymatch (pair. match ("718ak") # No pairs.
Displaymatch (pair. match ("354aa") # Pair of aces.
The output is as follows:
<Match: '20140901', groups = ('7',)>
None
<Match: '354a', groups = ('A',)>
In this example, use the regular expression r ". *(.). * \ 1 ", point number indicates any character; * indicates any repetition ;(.) is to match a character as a group ;. * Can contain any character. \ 1 represents the same character as the previous character group. If you want to obtain the duplicate character, you can use group to access it.
Cai junsheng QQ: 9073204 Shenzhen
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.