Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận The combination of all the above regex functions can be used to perform this particular task. In this we compile a regex and match it to group text and numbers separately into a tuple.
Python3
Đôi khi, chúng ta có một chuỗi, bao gồm văn bản và số [hoặc ngược lại], mà không có bất kỳ sự phân biệt cụ thể nào giữa hai. Có thể có một yêu cầu trong đó chúng tôi yêu cầu tách văn bản khỏi số. Hãy để thảo luận về những cách nhất định trong đó điều này có thể được thực hiện. & NBSP;
Phương thức số 1: Sử dụng re.compile [] + re.match [] + re.groups [] Sự kết hợp của tất cả các hàm Regex trên có thể được sử dụng để thực hiện nhiệm vụ cụ thể này. Trong đó, chúng tôi biên dịch một regex và khớp nó với nhóm và các số riêng biệt thành một tuple. & Nbsp;
import
re
test_str
=
"Geeks4321"
print
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']0____11
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']2
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']3
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']4
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']5
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']6
=
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']8
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']9
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']0
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']1
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']2221
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']4
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']3__
3import re
# Format is [[, ], ...]
ss = [["apple-12.34 ba33na fanc-14.23e-2yapple+45e5+67.56E+3",
['-12.34', '33', '-14.23e-2', '+45e5', '+67.56E+3']],
['hello X42 I\'m a Y-32.35 string Z30',
['42', '-32.35', '30']],
['he33llo 42 I\'m a 32 string -30',
['33', '42', '32', '-30']],
['h3110 23 cat 444.4 rabbit 11 2 dog',
['3110', '23', '444.4', '11', '2']],
['hello 12 hi 89',
['12', '89']],
['4',
['4']],
['I like 74,600 commas not,500',
['74,600', '500']],
['I like bad math 1+2=.001',
['1', '+2', '.001']]]
for s, r in ss:
rr = re.findall["[-+]?[.]?[\d]+[?:,\d\d\d]*[\.]?\d*[?:[eE][-+]?\d+]?", s]
if rr == r:
print['GOOD']
else:
print['WRONG', rr, 'should be', r]
=
5import re
# Format is [[, ], ...]
ss = [["apple-12.34 ba33na fanc-14.23e-2yapple+45e5+67.56E+3",
['-12.34', '33', '-14.23e-2', '+45e5', '+67.56E+3']],
['hello X42 I\'m a Y-32.35 string Z30',
['42', '-32.35', '30']],
['he33llo 42 I\'m a 32 string -30',
['33', '42', '32', '-30']],
['h3110 23 cat 444.4 rabbit 11 2 dog',
['3110', '23', '444.4', '11', '2']],
['hello 12 hi 89',
['12', '89']],
['4',
['4']],
['I like 74,600 commas not,500',
['74,600', '500']],
['I like bad math 1+2=.001',
['1', '+2', '.001']]]
for s, r in ss:
rr = re.findall["[-+]?[.]?[\d]+[?:,\d\d\d]*[\.]?\d*[?:[eE][-+]?\d+]?", s]
if rr == r:
print['GOOD']
else:
print['WRONG', rr, 'should be', r]
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']
print
7import re
# Format is [[, ], ...]
ss = [["apple-12.34 ba33na fanc-14.23e-2yapple+45e5+67.56E+3",
['-12.34', '33', '-14.23e-2', '+45e5', '+67.56E+3']],
['hello X42 I\'m a Y-32.35 string Z30',
['42', '-32.35', '30']],
['he33llo 42 I\'m a 32 string -30',
['33', '42', '32', '-30']],
['h3110 23 cat 444.4 rabbit 11 2 dog',
['3110', '23', '444.4', '11', '2']],
['hello 12 hi 89',
['12', '89']],
['4',
['4']],
['I like 74,600 commas not,500',
['74,600', '500']],
['I like bad math 1+2=.001',
['1', '+2', '.001']]]
for s, r in ss:
rr = re.findall["[-+]?[.]?[\d]+[?:,\d\d\d]*[\.]?\d*[?:[eE][-+]?\d+]?", s]
if rr == r:
print['GOOD']
else:
print['WRONG', rr, 'should be', r]
8 import re
# Format is [[, ], ...]
ss = [["apple-12.34 ba33na fanc-14.23e-2yapple+45e5+67.56E+3",
['-12.34', '33', '-14.23e-2', '+45e5', '+67.56E+3']],
['hello X42 I\'m a Y-32.35 string Z30',
['42', '-32.35', '30']],
['he33llo 42 I\'m a 32 string -30',
['33', '42', '32', '-30']],
['h3110 23 cat 444.4 rabbit 11 2 dog',
['3110', '23', '444.4', '11', '2']],
['hello 12 hi 89',
['12', '89']],
['4',
['4']],
['I like 74,600 commas not,500',
['74,600', '500']],
['I like bad math 1+2=.001',
['1', '+2', '.001']]]
for s, r in ss:
rr = re.findall["[-+]?[.]?[\d]+[?:,\d\d\d]*[\.]?\d*[?:[eE][-+]?\d+]?", s]
if rr == r:
print['GOOD']
else:
print['WRONG', rr, 'should be', r]
9import re
# Format is [[, ], ...]
ss = [["apple-12.34 ba33na fanc-14.23e-2yapple+45e5+67.56E+3",
['-12.34', '33', '-14.23e-2', '+45e5', '+67.56E+3']],
['hello X42 I\'m a Y-32.35 string Z30',
['42', '-32.35', '30']],
['he33llo 42 I\'m a 32 string -30',
['33', '42', '32', '-30']],
['h3110 23 cat 444.4 rabbit 11 2 dog',
['3110', '23', '444.4', '11', '2']],
['hello 12 hi 89',
['12', '89']],
['4',
['4']],
['I like 74,600 commas not,500',
['74,600', '500']],
['I like bad math 1+2=.001',
['1', '+2', '.001']]]
for s, r in ss:
rr = re.findall["[-+]?[.]?[\d]+[?:,\d\d\d]*[\.]?\d*[?:[eE][-+]?\d+]?", s]
if rr == r:
print['GOOD']
else:
print['WRONG', rr, 'should be', r]
import
0 import
1The original string is : Geeks4321
The tuple after the split of string and number : ['Geeks', '4321']
3 The slight modification of regex can provide
the flexibility to reduce the number of regex functions required to perform this particular task. The findall function is alone enough for this task.
Python3
Đôi khi, chúng ta có một chuỗi, bao gồm văn bản và số [hoặc ngược lại], mà không có bất kỳ sự phân biệt cụ thể nào giữa hai. Có thể có một yêu cầu trong đó chúng tôi yêu cầu tách văn bản khỏi số. Hãy để thảo luận về những cách nhất định trong đó điều này có thể được thực hiện. & NBSP;
Phương thức số 1: Sử dụng re.compile [] + re.match [] + re.groups [] Sự kết hợp của tất cả các hàm Regex trên có thể được sử dụng để thực hiện nhiệm vụ cụ thể này. Trong đó, chúng tôi biên dịch một regex và khớp nó với nhóm và các số riêng biệt thành một tuple. & Nbsp;
import
re
test_str
=
"Geeks4321"
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']6
=
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']8
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']9
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']0
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']1
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']2221
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']4
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']3__
3import re
# Format is [[, ], ...]
ss = [["apple-12.34 ba33na fanc-14.23e-2yapple+45e5+67.56E+3",
['-12.34', '33', '-14.23e-2', '+45e5', '+67.56E+3']],
['hello X42 I\'m a Y-32.35 string Z30',
['42', '-32.35', '30']],
['he33llo 42 I\'m a 32 string -30',
['33', '42', '32', '-30']],
['h3110 23 cat 444.4 rabbit 11 2 dog',
['3110', '23', '444.4', '11', '2']],
['hello 12 hi 89',
['12', '89']],
['4',
['4']],
['I like 74,600 commas not,500',
['74,600', '500']],
['I like bad math 1+2=.001',
['1', '+2', '.001']]]
for s, r in ss:
rr = re.findall["[-+]?[.]?[\d]+[?:,\d\d\d]*[\.]?\d*[?:[eE][-+]?\d+]?", s]
if rr == r:
print['GOOD']
else:
print['WRONG', rr, 'should be', r]
=
5import re
# Format is [[, ], ...]
ss = [["apple-12.34 ba33na fanc-14.23e-2yapple+45e5+67.56E+3",
['-12.34', '33', '-14.23e-2', '+45e5', '+67.56E+3']],
['hello X42 I\'m a Y-32.35 string Z30',
['42', '-32.35', '30']],
['he33llo 42 I\'m a 32 string -30',
['33', '42', '32', '-30']],
['h3110 23 cat 444.4 rabbit 11 2 dog',
['3110', '23', '444.4', '11', '2']],
['hello 12 hi 89',
['12', '89']],
['4',
['4']],
['I like 74,600 commas not,500',
['74,600', '500']],
['I like bad math 1+2=.001',
['1', '+2', '.001']]]
for s, r in ss:
rr = re.findall["[-+]?[.]?[\d]+[?:,\d\d\d]*[\.]?\d*[?:[eE][-+]?\d+]?", s]
if rr == r:
print['GOOD']
else:
print['WRONG', rr, 'should be', r]
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']
print
7import re
# Format is [[, ], ...]
ss = [["apple-12.34 ba33na fanc-14.23e-2yapple+45e5+67.56E+3",
['-12.34', '33', '-14.23e-2', '+45e5', '+67.56E+3']],
['hello X42 I\'m a Y-32.35 string Z30',
['42', '-32.35', '30']],
['he33llo 42 I\'m a 32 string -30',
['33', '42', '32', '-30']],
['h3110 23 cat 444.4 rabbit 11 2 dog',
['3110', '23', '444.4', '11', '2']],
['hello 12 hi 89',
['12', '89']],
['4',
['4']],
['I like 74,600 commas not,500',
['74,600', '500']],
['I like bad math 1+2=.001',
['1', '+2', '.001']]]
for s, r in ss:
rr = re.findall["[-+]?[.]?[\d]+[?:,\d\d\d]*[\.]?\d*[?:[eE][-+]?\d+]?", s]
if rr == r:
print['GOOD']
else:
print['WRONG', rr, 'should be', r]
8 import re
# Format is [[, ], ...]
ss = [["apple-12.34 ba33na fanc-14.23e-2yapple+45e5+67.56E+3",
['-12.34', '33', '-14.23e-2', '+45e5', '+67.56E+3']],
['hello X42 I\'m a Y-32.35 string Z30',
['42', '-32.35', '30']],
['he33llo 42 I\'m a 32 string -30',
['33', '42', '32', '-30']],
['h3110 23 cat 444.4 rabbit 11 2 dog',
['3110', '23', '444.4', '11', '2']],
['hello 12 hi 89',
['12', '89']],
['4',
['4']],
['I like 74,600 commas not,500',
['74,600', '500']],
['I like bad math 1+2=.001',
['1', '+2', '.001']]]
for s, r in ss:
rr = re.findall["[-+]?[.]?[\d]+[?:,\d\d\d]*[\.]?\d*[?:[eE][-+]?\d+]?", s]
if rr == r:
print['GOOD']
else:
print['WRONG', rr, 'should be', r]
9import re
# Format is [[, ], ...]
ss = [["apple-12.34 ba33na fanc-14.23e-2yapple+45e5+67.56E+3",
['-12.34', '33', '-14.23e-2', '+45e5', '+67.56E+3']],
['hello X42 I\'m a Y-32.35 string Z30',
['42', '-32.35', '30']],
['he33llo 42 I\'m a 32 string -30',
['33', '42', '32', '-30']],
['h3110 23 cat 444.4 rabbit 11 2 dog',
['3110', '23', '444.4', '11', '2']],
['hello 12 hi 89',
['12', '89']],
['4',
['4']],
['I like 74,600 commas not,500',
['74,600', '500']],
['I like bad math 1+2=.001',
['1', '+2', '.001']]]
for s, r in ss:
rr = re.findall["[-+]?[.]?[\d]+[?:,\d\d\d]*[\.]?\d*[?:[eE][-+]?\d+]?", s]
if rr == r:
print['GOOD']
else:
print['WRONG', rr, 'should be', r]
import
0 import
1The original string is : Geeks4321
The tuple after the split of string and number : ['Geeks', '4321']
3
Python3
Phương thức số 1: Sử dụng re.compile [] + re.match [] + re.groups [] Sự kết hợp của tất cả các hàm Regex trên có thể được sử dụng để thực hiện nhiệm vụ cụ thể này. Trong đó, chúng tôi biên dịch một regex và khớp nó với nhóm và các số riêng biệt thành một tuple. & Nbsp;
import
re
"Geeks4321"
2=
"Geeks4321"
4
"Geeks4321"
5=
"Geeks4321"
4
"Geeks4321"
8=
print
0
test_str
=
"Geeks4321"
print
5print
6print
7
print
8"Geeks4321"
5
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']3
=
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']02
print
5
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']04
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']05
print
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']0____11
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']2
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']3
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']4
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']5
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']11
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']12
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']6
=
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']8
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']9
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']0
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']1
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']2221
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']4
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']3__
3import re
# Format is [[, ], ...]
ss = [["apple-12.34 ba33na fanc-14.23e-2yapple+45e5+67.56E+3",
['-12.34', '33', '-14.23e-2', '+45e5', '+67.56E+3']],
['hello X42 I\'m a Y-32.35 string Z30',
['42', '-32.35', '30']],
['he33llo 42 I\'m a 32 string -30',
['33', '42', '32', '-30']],
['h3110 23 cat 444.4 rabbit 11 2 dog',
['3110', '23', '444.4', '11', '2']],
['hello 12 hi 89',
['12', '89']],
['4',
['4']],
['I like 74,600 commas not,500',
['74,600', '500']],
['I like bad math 1+2=.001',
['1', '+2', '.001']]]
for s, r in ss:
rr = re.findall["[-+]?[.]?[\d]+[?:,\d\d\d]*[\.]?\d*[?:[eE][-+]?\d+]?", s]
if rr == r:
print['GOOD']
else:
print['WRONG', rr, 'should be', r]
=
5import re
# Format is [[, ], ...]
ss = [["apple-12.34 ba33na fanc-14.23e-2yapple+45e5+67.56E+3",
['-12.34', '33', '-14.23e-2', '+45e5', '+67.56E+3']],
['hello X42 I\'m a Y-32.35 string Z30',
['42', '-32.35', '30']],
['he33llo 42 I\'m a 32 string -30',
['33', '42', '32', '-30']],
['h3110 23 cat 444.4 rabbit 11 2 dog',
['3110', '23', '444.4', '11', '2']],
['hello 12 hi 89',
['12', '89']],
['4',
['4']],
['I like 74,600 commas not,500',
['74,600', '500']],
['I like bad math 1+2=.001',
['1', '+2', '.001']]]
for s, r in ss:
rr = re.findall["[-+]?[.]?[\d]+[?:,\d\d\d]*[\.]?\d*[?:[eE][-+]?\d+]?", s]
if rr == r:
print['GOOD']
else:
print['WRONG', rr, 'should be', r]
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']
Điều này còn hơn một chút, nhưng bạn có thể mở rộng biểu thức regex để tính toán ký hiệu khoa học quá.
import re
# Format is [[, ], ...]
ss = [["apple-12.34 ba33na fanc-14.23e-2yapple+45e5+67.56E+3",
['-12.34', '33', '-14.23e-2', '+45e5', '+67.56E+3']],
['hello X42 I\'m a Y-32.35 string Z30',
['42', '-32.35', '30']],
['he33llo 42 I\'m a 32 string -30',
['33', '42', '32', '-30']],
['h3110 23 cat 444.4 rabbit 11 2 dog',
['3110', '23', '444.4', '11', '2']],
['hello 12 hi 89',
['12', '89']],
['4',
['4']],
['I like 74,600 commas not,500',
['74,600', '500']],
['I like bad math 1+2=.001',
['1', '+2', '.001']]]
for s, r in ss:
rr = re.findall["[-+]?[.]?[\d]+[?:,\d\d\d]*[\.]?\d*[?:[eE][-+]?\d+]?", s]
if rr == r:
print['GOOD']
else:
print['WRONG', rr, 'should be', r]
Cho tất cả tốt!
Ngoài ra, bạn có thể nhìn vào Regex tích hợp keo AWS