The difference between Python full stack--6.1-match-search-findall-group (s) and the calculator instance

Source: Internet
Author: User
Tags microsoft dynamics

The difference between Python full stack--6.1-match-search-findall-group (s) and the calculator instance
Match, search, FindAll, group (s) differences
1
2
3
4
5

Import re
# match FindAll often use
# Re.match () #从开头匹配, no match to object returns none
# Re.search () #浏览全部字符, matches the first rule-compliant string
# Re.findall () # Place everything that matches to a list
There are two kinds of match.

-------have a grouping------take a match to the regular to take some of its contents again
1
2
3
4
5
6

Origin = "Hello Alex sadf Dsaf"
r = Re.match ("(h) \w+", origin)
Print (R.group ()) # Hello get match all results
Print (R.groups ()) # (' H ',) #获取模型中匹配到的分组 are empty tuples without grouping
r = Re.match (? P&LT;N1&GT;H) (? p<n2>\w+) ", origin) #获取模型中匹配到的分组中所有执行力key的组? P<key>value {' N2 ': ' Ello ', ' N1 ': ' H '}
Print (R.groupdict ()) #? P<key>value {' N2 ': ' Ello ', ' N1 ': ' H '}

-------No grouping------
1
2
3
4

r = Re.match ("h\w+", origin)
Print (R.group ()) # get match all results hello
Print (R.groups ()) #获取模型中匹配到的分组 ()
Print (R.groupdict ()) #获取模型中匹配到的分组 {}
Second, groups, group, groupdict
1
2
3
4

Print (R.group ()) # Hello get match all results
Print (R.groups ()) # (' H ',) #获取模型中匹配到的分组 are empty tuples without grouping
r = Re.match (? P&LT;N1&GT;H) (? p<n2>\w+) ", origin) #获取模型中匹配到的分组中所有执行力key的组? P<key>value {' N2 ': ' Ello ', ' N1 ': ' H '}
Print (R.groupdict ()) #? P<key>value {' N2 ': ' Ello ', ' N1 ': ' H '}
Three, search two kinds of situation

Search does not match from the beginning, but rather matches the global entire string, and exits once it is met

-------have group------
1
2
3
4
5
6

Origin = "Hello Alex Alix BCD DSFA LEFG ABC 199"
r = Re.search ("A (\w+)", origin)
Print (R.group ()) #alex
Print (R.groups ()) # (' Lex ',)
r = Re.search (? P<key1>a) (? P<key2> (\w+)) ", origin)
Print (R.groupdict ()) #{' key1 ': ' A ', ' key2 ': ' Lex '}

-------No grouping------
1
2
3
4
5

Origin = "Hello Alex Alix BCD DSFA LEFG ABC 199"
r = Re.search ("ali\w+", origin)
Print (R.group ()) #alix
Print (R.groups ()) # ()
Print (R.groupdict ()) #{}
Iv. FindAll

# 1 after matching to, pull away, continue to match from the next character
1
2
3

Origin = "A2b3c4d5"
A=re.findall ("\d+\w\d+", origin) #[' 2b3 ', ' 4d5 ']
Print (a)

# 2 null values will also match each character, and after the end, continue to match once more
1
2

num = "ASD"
Print (Re.findall ("", num)) # [', ', ', ', ']

# no grouping
1
2
3
4
5

Origin = "Hello Alex Alix BCD DSFA LEFG ABC 199"
Print (Re.findall ("a\w+", origin) # [' Alex ', ' Alix ', ' ABC ']
Print (Re.findall ("(a\w+)", origin)) #[' Alex ', ' Alix ', ' ABC ']
Print (Re.findall ("A (\w+)", origin)) #组 groups [' Lex ', ' lix ', ' BC ']
Print (Re.findall ("(a) (\w+) (x)", origin)) # [(' A ', ' Le ', ' x '), (' A ', ' Li ', ' X ')] Three groups matched to the tuple to be placed into the list as an element


Features of FindAll
1
2
3
4
5
6
7
8
9
10
11

Group Matching
* Greedy match: For example, the following ASD is a grouping, and once the ASD begins to encounter a string, if
The back or the ASD will also match, which is the greedy match.
FindAll Features:
1 There are several groupings that return several things: and put the returned content in a tuple as an element of the list.
2 that although * matched to two ASD, but because it is greedy virtual match out, there is a grouping, so,
Just, take one.
3 FindAll defaults to only the last set of matches, so it returns only the last set of ASD
4 FindAll If the regular can have an empty match, then the matching string will eventually match to an empty
More than 5 groupings matched to a tuple, as an element of the list
6 multiple groupings, lookup order, outside to inside, left to right ((\w) (\w)) {2}, first 2 letters found, and then local grouping in these two letters

# 1 Features 1 2 3 4
1

Print (Re.findall (R ' (ASD) * ', ' asdasd ')) #[' ASD ', ']

# 2 below, the default + can be matched to 1ASD2ASD for the first time, but since there are only 1 groupings in front, only the next 4 bits are returned.
1
2

n = Re.findall ("(\DASD) +", "1asd2asdp2asds") # [' 2asd ', ' 2ASD ']
Print (n)

# 3 as follows, the default * can be matched to 1ASD2ASD for the first time but only 1 packets in front of it, only the next 4 bits are returned.

#但是 * can also be expressed 0 times, so again encountered p when, empty can match, and, finally there is a s also match to empty, the end of the default will also match to empty
1
2

n = Re.findall ("(\DASD) *", "1asd2asdp2asds") # [' 2asd ', ' ', ' 2asd ', ', ']
Print (n)

#4 matches, up to four letters, so Alex finds it, but only one group, so it only returns the last \w X
1
2
3

A = "Alex"
n = Re.findall ("(\w) {4}", a)
Print (n) # [' X ']

#5 here FindAll match grouping is a letter matched to, and * is greedy match, four letters are matched to take four letters, and the regular group only one, so only take the last
1
2
3

A = "Alex"
n = Re.findall ("(\w) *", a)
Print (n) # [' X ', ']

#6 because from outside to inside, so match two letter of Al match to, ex also match to, external Group 1, so ex, while inside from left to right again will match.
1
2
3

A = "Alex"
n = Re.findall ("((\w) (\w)) {2,}", a)
Print (n) # [(' Ex ', ' e ', ' X ')]

#7 will match ax Alex Alex ACD but the greedy matching group is only 1 so ax ax Microsoft Dynamics AX AD
1
2
3

Origin = "Hello ax Lex bed Alex Lge" Alex ACD 19 "
n = Re.findall ("(a) (\w) *", origin)
Print (n) #[(' A ', ' X '), (' A ', ' X '), (' A ', ' X '), (' A ', ' d ')]

Example Python writing calculator
Calculator

Results
1
2
3
4

C:\Python35\python3.exe e:/py_test/s3_py/real_cal_liujianzuo.py
The formula is: 1-2* ((60-30+ (1-40/5*5+3-2*5/3) * (9-2*5/3+7/3*99/4*2998+10*568/14))-( -4*3)/(16-3*2))
The result is: 13652214.56825397
read:0.001188 s

Version1
Version 1

Version2
Version 2

Version3
View Code

The difference between Python full stack--6.1-match-search-findall-group (s) and the calculator instance

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.