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 Regex in Python
Bàn luận re.search[] and re.match[] both are functions of re module in python. These functions are very efficient and fast for searching in strings. The function searches for some substring in a string and returns a match object if found, else it returns none.
Điều kiện tiên quyết: Regex trong Pythonre.match[] searches only from the beginning
of the string and return match object if found. But if a match of substring is found somewhere in the middle of the string, it returns none.
While re.search[] searches for the whole string even if the string contains multi-lines and tries to find a match of the substring in all the lines of string.
Cả re.Search [] và re.Match [] đều là các hàm của mô -đun RE trong Python. Các chức năng này rất hiệu quả và nhanh chóng để tìm kiếm trong chuỗi. Hàm tìm kiếm một số chuỗi con trong một chuỗi và trả về một đối tượng khớp nếu tìm thấy, nếu không nó sẽ trả về không.
Python3
Có một sự khác biệt giữa việc sử dụng cả hai chức năng. Cả hai trả về trận đấu đầu tiên của một chuỗi con được tìm thấy trong chuỗi, nhưng re.match [] chỉ tìm kiếm từ đầu chuỗi và trả về đối tượng đối sánh nếu tìm thấy. Nhưng nếu một trận đấu của chuỗi con được tìm thấy ở đâu đó ở giữa chuỗi, nó sẽ không trả về. trong tất cả các dòng của chuỗi. & nbsp;
Substring
=
'string'
Ví dụ: & nbsp;
String2
=
# example code:
string_with_newlines = """something
someotherthing"""
import re
print re.match['some', string_with_newlines] # matches
print re.match['someother',
string_with_newlines] # won't match
print re.match['^someother', string_with_newlines,
re.MULTILINE] # also won't match
print re.search['someother',
string_with_newlines] # finds something
print re.search['^someother', string_with_newlines,
re.MULTILINE] # also finds something
m = re.compile['thing$', re.MULTILINE]
print m.match[string_with_newlines] # no match
print m.match[string_with_newlines, pos=4] # matches
print m.search[string_with_newlines,
re.MULTILINE] # also matches
1# example code:
string_with_newlines = """something
someotherthing"""
import re
print re.match['some', string_with_newlines] # matches
print re.match['someother',
string_with_newlines] # won't match
print re.match['^someother', string_with_newlines,
re.MULTILINE] # also won't match
print re.search['someother',
string_with_newlines] # finds something
print re.search['^someother', string_with_newlines,
re.MULTILINE] # also finds something
m = re.compile['thing$', re.MULTILINE]
print m.match[string_with_newlines] # no match
print m.match[string_with_newlines, pos=4] # matches
print m.search[string_with_newlines,
re.MULTILINE] # also matches
2# example code:
string_with_newlines = """something
someotherthing"""
import re
print re.match['some', string_with_newlines] # matches
print re.match['someother',
string_with_newlines] # won't match
print re.match['^someother', string_with_newlines,
re.MULTILINE] # also won't match
print re.search['someother',
string_with_newlines] # finds something
print re.search['^someother', string_with_newlines,
re.MULTILINE] # also finds something
m = re.compile['thing$', re.MULTILINE]
print m.match[string_with_newlines] # no match
print m.match[string_with_newlines, pos=4] # matches
print m.search[string_with_newlines,
re.MULTILINE] # also matches
1# example code:
string_with_newlines = """something
someotherthing"""
import re
print re.match['some', string_with_newlines] # matches
print re.match['someother',
string_with_newlines] # won't match
print re.match['^someother', string_with_newlines,
re.MULTILINE] # also won't match
print re.search['someother',
string_with_newlines] # finds something
print re.search['^someother', string_with_newlines,
re.MULTILINE] # also finds something
m = re.compile['thing$', re.MULTILINE]
print m.match[string_with_newlines] # no match
print m.match[string_with_newlines, pos=4] # matches
print m.search[string_with_newlines,
re.MULTILINE] # also matches
4# example code:
string_with_newlines = """something
someotherthing"""
import re
print re.match['some', string_with_newlines] # matches
print re.match['someother',
string_with_newlines] # won't match
print re.match['^someother', string_with_newlines,
re.MULTILINE] # also won't match
print re.search['someother',
string_with_newlines] # finds something
print re.search['^someother', string_with_newlines,
re.MULTILINE] # also finds something
m = re.compile['thing$', re.MULTILINE]
print m.match[string_with_newlines] # no match
print m.match[string_with_newlines, pos=4] # matches
print m.search[string_with_newlines,
re.MULTILINE] # also matches
1# example code:
string_with_newlines = """something
someotherthing"""
import re
print re.match['some', string_with_newlines] # matches
print re.match['someother',
string_with_newlines] # won't match
print re.match['^someother', string_with_newlines,
re.MULTILINE] # also won't match
print re.search['someother',
string_with_newlines] # finds something
print re.search['^someother', string_with_newlines,
re.MULTILINE] # also finds something
m = re.compile['thing$', re.MULTILINE]
print m.match[string_with_newlines] # no match
print m.match[string_with_newlines, pos=4] # matches
print m.search[string_with_newlines,
re.MULTILINE] # also matches
6# example code:
string_with_newlines = """something
someotherthing"""
import re
print re.match['some', string_with_newlines] # matches
print re.match['someother',
string_with_newlines] # won't match
print re.match['^someother', string_with_newlines,
re.MULTILINE] # also won't match
print re.search['someother',
string_with_newlines] # finds something
print re.search['^someother', string_with_newlines,
re.MULTILINE] # also finds something
m = re.compile['thing$', re.MULTILINE]
print m.match[string_with_newlines] # no match
print m.match[string_with_newlines, pos=4] # matches
print m.search[string_with_newlines,
re.MULTILINE] # also matches
1# example code:
string_with_newlines = """something
someotherthing"""
import re
print re.match['some', string_with_newlines] # matches
print re.match['someother',
string_with_newlines] # won't match
print re.match['^someother', string_with_newlines,
re.MULTILINE] # also won't match
print re.search['someother',
string_with_newlines] # finds something
print re.search['^someother', string_with_newlines,
re.MULTILINE] # also finds something
m = re.compile['thing$', re.MULTILINE]
print m.match[string_with_newlines] # no match
print m.match[string_with_newlines, pos=4] # matches
print m.search[string_with_newlines,
re.MULTILINE] # also matches
8import
re
None
String1
=
- Đầu ra: & nbsp; is returning match object and implies that first match found at index 69.
- Kết luận: & nbsp; is returning none because match exists in the second line of the string and re.match[] only works if the match is found at the beginning of the string.
- Re.Search [] đang trả về đối tượng khớp và ngụ ý rằng trận đấu đầu tiên được tìm thấy tại Index 69. is used to ignore the case sensitivity in the strings.
- re.match [] không trả về không vì kết hợp tồn tại trong dòng thứ hai của chuỗi và re.match [] chỉ hoạt động nếu trận đấu được tìm thấy ở đầu chuỗi. & nbsp;re.search[] and re.match[] returns only the first occurrence of a substring in the string and ignore others.
# example code:
string_with_newlines = """something
someotherthing"""
import re
print re.match['some', string_with_newlines] # matches
print re.match['someother',
string_with_newlines] # won't match
print re.match['^someother', string_with_newlines,
re.MULTILINE] # also won't match
print re.search['someother',
string_with_newlines] # finds something
print re.search['^someother', string_with_newlines,
re.MULTILINE] # also finds something
m = re.compile['thing$', re.MULTILINE]
print m.match[string_with_newlines] # no match
print m.match[string_with_newlines, pos=4] # matches
print m.search[string_with_newlines,
re.MULTILINE] # also matches
9 được neo ở đầu chuỗi. Điều đó không liên quan gì đến Newlines, vì vậy nó không giống như sử dụng import
0 trong mẫu.Như tài liệu Re.Match đã nói:
Nếu số không hoặc nhiều ký tự ở đầu chuỗi khớp với mẫu biểu thức chính quy, hãy trả về một thể hiện
import
1 tương ứng. Trả vềimport
2 nếu chuỗi không khớp với mẫu; Lưu ý rằng điều này khác với một trận đấu có độ dài bằng không.beginning of string match the regular expression pattern, return a correspondingimport
1 instance. Returnimport
2 if the string does not match the pattern; note that this is different from a zero-length match.Lưu ý: Nếu bạn muốn xác định vị trí đối sánh ở bất cứ đâu trong chuỗi, hãy sử dụng
import
3 thay thế.
import
4 Tìm kiếm toàn bộ chuỗi, như tài liệu nói:
Quét qua chuỗi Tìm kiếm một vị trí nơi mẫu biểu thức chính quy tạo ra một khớp và trả về một thể hiện
import
1 tương ứng. Trả vềimport
2 Nếu không có vị trí nào trong chuỗi khớp với mẫu; Lưu ý rằng điều này khác với việc tìm một trận đấu có độ dài bằng không tại một số điểm trong chuỗi. looking for a location where the regular expression pattern produces a match, and return a correspondingimport
1 instance. Returnimport
2 if no position in the string matches the pattern; note that this is different from finding a zero-length match at some point in the string.
Vì vậy, nếu bạn cần khớp ở đầu chuỗi hoặc để khớp toàn bộ chuỗi sử dụng import
7. Nó nhanh hơn. Nếu không, sử dụng import
8.
Tài liệu có một phần cụ thể cho import
7 so với import
8 cũng bao gồm các chuỗi đa dòng:
Python cung cấp hai hoạt động nguyên thủy khác nhau dựa trên các biểu thức thông thường:
import
7 chỉ kiểm tra một trận đấu ở đầu chuỗi, trong khiimport
8 kiểm tra đối sánh ở bất cứ đâu trong chuỗi [đây là những gì Perl làm theo mặc định].only at the beginning of the string, whileimport
8 checks for a match anywhere in the string [this is what Perl does by default].Lưu ý rằng
import
7 có thể khác vớiimport
8 ngay cả khi sử dụng biểu thức chính quy bắt đầu bằngre
5:re
5 chỉ khớp với đầu chuỗi hoặc ở chế độre
7 cũng ngay lập tức theo một dòng mới. Hoạt động của ____ ____27, chỉ thành công nếu mẫu phù hợp khi bắt đầu chuỗi bất kể chế độ, hoặc ở vị trí bắt đầu được đưa ra bởi đối sốre
9 tùy chọn bất kể dòng mới có đi trước nó hay không.start of the string regardless of mode, or at the starting position given by the optionalre
9 argument regardless of whether a newline precedes it.
Bây giờ, nói đủ. Thời gian để xem một số mã ví dụ:
# example code:
string_with_newlines = """something
someotherthing"""
import re
print re.match['some', string_with_newlines] # matches
print re.match['someother',
string_with_newlines] # won't match
print re.match['^someother', string_with_newlines,
re.MULTILINE] # also won't match
print re.search['someother',
string_with_newlines] # finds something
print re.search['^someother', string_with_newlines,
re.MULTILINE] # also finds something
m = re.compile['thing$', re.MULTILINE]
print m.match[string_with_newlines] # no match
print m.match[string_with_newlines, pos=4] # matches
print m.search[string_with_newlines,
re.MULTILINE] # also matches