Hướng dẫn what is the difference between match() and search() methods in python? - sự khác biệt giữa phương thức match () và search () trong python là gì?

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • 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
    
    8

    import re 

    
    None
    
    

    String1 = 

    1. Đầu ra: & nbsp; is returning match object and implies that first match found at index 69.
    2. 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. 
    3. 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. 
    4. 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 import0 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 import1 tương ứng. Trả về import2 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 corresponding import1 instance. Return import2 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 import3 thay thế.

    import4 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 import1 tương ứng. Trả về import2 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 corresponding import1 instance. Return import2 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 import7. Nó nhanh hơn. Nếu không, sử dụng import8.

    Tài liệu có một phần cụ thể cho import7 so với import8 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: import7 chỉ kiểm tra một trận đấu ở đầu chuỗi, trong khi import8 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, while import8 checks for a match anywhere in the string (this is what Perl does by default).

    Lưu ý rằng import7 có thể khác với import8 ngay cả khi sử dụng biểu thức chính quy bắt đầu bằng re5: re5 chỉ khớp với đầu chuỗi hoặc ở chế độ re7 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ố re9 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 optional re9 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
    

    Sự khác biệt giữa trận đấu và tìm kiếm là gì?

    Tìm kiếm quét qua toàn bộ chuỗi.Match chỉ quét phần đầu của chuỗi. match scans only the beginning of the string.

    Sự khác biệt giữa các phương thức tìm kiếm () và findall () trong Python là gì?

    Mô -đun findall () được sử dụng để tìm kiếm tất cả các lần xuất hiện của các loại phù hợp với một mẫu nhất định. Trong mô -đun tương phản, tìm kiếm () sẽ chỉ trả về lần xuất hiện đầu tiên phù hợp với mẫu được chỉ định.Findall () sẽ lặp lại trên tất cả các dòng của tệp và sẽ trả về tất cả các trận đấu không chồng chéo của mẫu trong một bước duy nhất. In contrast, search() module will only return the first occurrence that matches the specified pattern. findall() will iterate over all the lines of the file and will return all non-overlapping matches of pattern in a single step.

    Sự khác biệt giữa các phương thức tìm kiếm () và findall () của biểu thức chính quy với các nhóm là gì?

    Ở đây bạn có thể thấy rằng, phương thức search () có thể tìm thấy một mẫu từ bất kỳ vị trí nào của chuỗi. Re.findall () giúp có được danh sách tất cả các mẫu phù hợp.Nó tìm kiếm từ đầu hoặc kết thúc của chuỗi đã cho.search() method is able to find a pattern from any position of the string. The re. findall() helps to get a list of all matching patterns. It searches from start or end of the given string.

    Match () trong Python là gì?

    khớp (mẫu, chuỗi): Phương thức này tìm thấy khớp nếu nó xảy ra khi bắt đầu chuỗi.Ví dụ: Gọi Match () trên chuỗi 'Hướng dẫn TP TP' và tìm kiếm một mẫu 'TP' sẽ khớp.Tuy nhiên, nếu chúng ta chỉ tìm kiếm các hướng dẫn, mẫu sẽ không khớp.This method finds match if it occurs at start of the string. For example, calling match() on the string 'TP Tutorials Point TP' and looking for a pattern 'TP' will match. However, if we look for only Tutorials, the pattern will not match.