Ví dụ đối sánh lại python

Tôi muốn có thể tìm thấy kết quả trùng khớp thứ hai trong dữ liệu sê-ri bằng cách sử dụng lại. phương pháp tìm kiếm. Cho đến nay tôi đã có thể sử dụng lại. tìm kiếm biểu thức chính quy để tìm trận đấu đầu tiên

Nội dung chính Hiển thị

  • Làm cách nào để khớp một chuỗi cụ thể trong Python?
  • Match[] làm gì trong Python?
  • Làm cách nào để khớp một chuỗi với một chuỗi khác?
  • Làm thế nào để bạn in một đối tượng phù hợp trong Python?

Làm cách nào để tôi tìm kiếm các chữ cái PR và in các chữ số ở bên phải của nó sau khi tìm thấy kết quả khớp đầu tiên trong cùng một chuỗi nối tiếp?

Đoạn mã dưới đây hiển thị kết nối với thiết bị thông qua cổng COM. Lưu trữ dữ liệu thành nối tiếp và tìm kiếm các chữ số ở bên phải của từ 'ID' trên cùng một dòng

dữ liệu nối tiếp. aassddffggID. 12345qqwwerrttPR. 54321zzxxxcc

ser = serial.Serial['COM1', 115200, timeout=0, parity=serial.PARITY_NONE, 
stopbits=serial.STOPBITS_ONE, rtscts=0]
print[ser]
time.sleep[0.5]
serial = ser.read[9999]

match = re.search['ID:[\d*]', serial]
print[match.group[0]]

Cập nhật

Tôi muốn sửa đổi điều này lại. tìm kiếm để cho phép các tham số được chuyển đến ID và PR. Tôi đã có thể chuyển một giá trị cho ID nhưng dường như không thể nhận các giá trị được chuyển cho cả hai mà không gặp lỗi

Bit này hoạt động

id = 12345 pr = 54321

if re.search[r"ID:{}".format[id], serial]
    print["ID match found"]
else:
    print["ID match not found"]

#output
"ID match found"

#For both ID and PR I've tried
if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
    print["PR match found"]
else:
    print["PR match not found"]

#output
"SyntaxError: invalid syntax" 

Ghi chú. Nó không giống như dấu phẩy đảo ngược sau dấu ngoặc nhọn đã đóng sau PR. {}

Bất kỳ đề xuất về thay đổi cú pháp?

Điều kiện tiên quyết. Python RegEx

Bạn có thể quen với việc tìm kiếm văn bản bằng cách nhấn ctrl-F và nhập các từ bạn đang tìm kiếm. Biểu thức chính quy tiến thêm một bước. Chúng cho phép bạn chỉ định một mẫu văn bản để tìm kiếm.
Biểu thức chính quy, gọi tắt là biểu thức chính quy, là mô tả cho một mẫu văn bản. Ví dụ: \d trong biểu thức chính quy là viết tắt của ký tự chữ số — nghĩa là bất kỳ chữ số đơn nào từ 0 đến 9.

  • Biểu thức chính quy sau được sử dụng trong Python để khớp một chuỗi gồm ba số, dấu gạch nối, ba số khác, dấu gạch nối khác và bốn số.
    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
  • Biểu thức chính quy có thể phức tạp hơn nhiều. Ví dụ: thêm số 3 vào dấu ngoặc nhọn [{3}] sau một mẫu giống như nói: “ Ghép mẫu này ba lần. ” Vì vậy, regex
    \d{3}-\d{3}-\d{4}
    ngắn hơn một chút

    [Nó phù hợp với định dạng số điện thoại chính xác. ]

Tạo đối tượng Regex

Tất cả các hàm regex trong Python đều có trong mô-đun re

import re

Để tạo đối tượng Regex khớp với mẫu số điện thoại, hãy nhập thông tin sau vào trình bao tương tác

phoneNumRegex = re.compile[r'\d\d\d-\d\d\d-\d\d\d\d']

Bây giờ biến phoneNumRegex chứa một đối tượng Regex

Đối sánh các đối tượng regex

Phương thức search[] của đối tượng Regex tìm kiếm chuỗi mà nó được truyền để tìm bất kỳ kết quả khớp nào với biểu thức chính quy. Các đối tượng khớp có một phương thức group[] sẽ trả về văn bản khớp thực tế từ chuỗi đã tìm kiếm

Any other string would not match the pattern.
\d\d\d-\d\d\d-\d\d\d\d
4
Any other string would not match the pattern.
\d\d\d-\d\d\d-\d\d\d\d
5

Any other string would not match the pattern.
\d\d\d-\d\d\d-\d\d\d\d
6
Any other string would not match the pattern.
\d\d\d-\d\d\d-\d\d\d\d
7
Any other string would not match the pattern.
\d\d\d-\d\d\d-\d\d\d\d
8
Any other string would not match the pattern.
\d\d\d-\d\d\d-\d\d\d\d
9
\d{3}-\d{3}-\d{4}
0
\d{3}-\d{3}-\d{4}
1
\d{3}-\d{3}-\d{4}
2

\d{3}-\d{3}-\d{4}
3______27
\d{3}-\d{3}-\d{4}
5______36
\d{3}-\d{3}-\d{4}
2

\d{3}-\d{3}-\d{4}
8
\d{3}-\d{3}-\d{4}
9
import re
0
import re
1
import re
2

đầu ra

Any other string would not match the pattern.
\d\d\d-\d\d\d-\d\d\d\d
5

Các bước đối sánh biểu thức chính quy

Mặc dù có một số bước để sử dụng biểu thức chính quy trong Python, nhưng mỗi bước đều khá đơn giản

  1. Nhập mô-đun regex với nhập lại
  2. Tạo một đối tượng Regex với re. chức năng biên dịch[]. [Hãy nhớ sử dụng một chuỗi thô. ]
  3. Truyền chuỗi bạn muốn tìm kiếm vào phương thức search[] của đối tượng Regex. Điều này trả về một đối tượng Match
  4. Gọi phương thức group[] của đối tượng Match để trả về một chuỗi văn bản khớp thực tế
  5. Nhóm với dấu ngoặc đơn

    1. Đối tượng phù hợp. Giả sử bạn muốn tách mã vùng khỏi phần còn lại của số điện thoại. Thêm dấu ngoặc đơn sẽ tạo các nhóm trong regex. [\d\d\d]-[\d\d\d-\d\d\d\d]. Sau đó, bạn có thể sử dụng phương thức đối tượng khớp nhóm[] để lấy văn bản khớp từ chỉ một nhóm

      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      4
      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      5

      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      6
      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      7
      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      8
      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      9
      \d{3}-\d{3}-\d{4}
      0
      phoneNumRegex = re.compile[r'\d\d\d-\d\d\d-\d\d\d\d']
      0
      \d{3}-\d{3}-\d{4}
      2

      \d{3}-\d{3}-\d{4}
      3______27
      \d{3}-\d{3}-\d{4}
      5______36
      \d{3}-\d{3}-\d{4}
      2

      \d{3}-\d{3}-\d{4}
      8
      phoneNumRegex = re.compile[r'\d\d\d-\d\d\d-\d\d\d\d']
      8____59
      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      50

      ĐẦU RA

      import re
      4
    2. Truy xuất tất cả các nhóm cùng một lúc. Nếu bạn muốn truy xuất tất cả các nhóm cùng một lúc, hãy sử dụng phương thức groups[]—lưu ý dạng số nhiều của tên

      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      4
      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      5

      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      6
      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      7
      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      8
      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      9
      \d{3}-\d{3}-\d{4}
      0
      phoneNumRegex = re.compile[r'\d\d\d-\d\d\d-\d\d\d\d']
      0
      \d{3}-\d{3}-\d{4}
      2

      \d{3}-\d{3}-\d{4}
      3______27
      \d{3}-\d{3}-\d{4}
      5______36
      \d{3}-\d{3}-\d{4}
      2

      \d{3}-\d{3}-\d{4}
      8
      import re
      46

      ĐẦU RA

      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      1
    3. sử dụng mo. các nhóm. mo. groups[] sẽ trả về một bộ gồm nhiều giá trị, bạn có thể sử dụng thủ thuật gán nhiều giá trị để gán từng giá trị cho một biến riêng biệt, như trong Mã vùng sau, mainNumber = mo. nhóm [] dòng

      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      4
      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      5

      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      6
      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      7
      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      8
      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      9
      \d{3}-\d{3}-\d{4}
      0
      phoneNumRegex = re.compile[r'\d\d\d-\d\d\d-\d\d\d\d']
      0
      \d{3}-\d{3}-\d{4}
      2

      \d{3}-\d{3}-\d{4}
      3______27
      \d{3}-\d{3}-\d{4}
      5______36
      \d{3}-\d{3}-\d{4}
      2

      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      61
      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      7
      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      63

      \d{3}-\d{3}-\d{4}
      8
      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      65

      ĐẦU RA

      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      6
    4. Phù hợp với một dấu ngoặc đơn. Dấu ngoặc đơn có ý nghĩa đặc biệt trong biểu thức chính quy, nhưng bạn sẽ làm gì nếu cần khớp dấu ngoặc đơn trong văn bản của mình. Chẳng hạn, có thể các số điện thoại bạn đang cố so khớp có mã vùng được đặt trong ngoặc đơn. Trong trường hợp này, bạn cần thoát ký tự [ và ] bằng dấu gạch chéo ngược. Nhập thông tin sau vào vỏ tương tác

      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      4
      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      5

      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      6
      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      7
      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      8
      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      9
      \d{3}-\d{3}-\d{4}
      0
      if re.search[r"ID:{}".format[id], serial]
          print["ID match found"]
      else:
          print["ID match not found"]
      
      #output
      "ID match found"
      
      #For both ID and PR I've tried
      if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
          print["PR match found"]
      else:
          print["PR match not found"]
      
      #output
      "SyntaxError: invalid syntax" 
      
      03
      \d{3}-\d{3}-\d{4}
      2

      \d{3}-\d{3}-\d{4}
      3
      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      7
      \d{3}-\d{3}-\d{4}
      5
      if re.search[r"ID:{}".format[id], serial]
          print["ID match found"]
      else:
          print["ID match not found"]
      
      #output
      "ID match found"
      
      #For both ID and PR I've tried
      if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
          print["PR match found"]
      else:
          print["PR match not found"]
      
      #output
      "SyntaxError: invalid syntax" 
      
      08
      \d{3}-\d{3}-\d{4}
      2

      \d{3}-\d{3}-\d{4}
      8
      phoneNumRegex = re.compile[r'\d\d\d-\d\d\d-\d\d\d\d']
      8____59
      Any other string would not match the pattern.
      \d\d\d-\d\d\d-\d\d\d\d
      50

      ĐẦU RA

      if re.search[r"ID:{}".format[id], serial]
          print["ID match found"]
      else:
          print["ID match not found"]
      
      #output
      "ID match found"
      
      #For both ID and PR I've tried
      if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
          print["PR match found"]
      else:
          print["PR match not found"]
      
      #output
      "SyntaxError: invalid syntax" 
      
      0

      Các ký tự thoát \[ và \] trong chuỗi thô được chuyển tới. compile[] sẽ khớp với các ký tự trong ngoặc đơn thực tế

    Kết hợp nhiều nhóm với đường ống

    Các. nhân vật được gọi là một đường ống. Bạn có thể sử dụng nó ở bất cứ đâu bạn muốn khớp với một trong nhiều biểu thức. Ví dụ: biểu thức chính quy r'Batman. Tina Fey’ sẽ phù hợp với ‘Batman’ hoặc ‘Tina Fey’

    Khi cả Batman và Tina Fey xuất hiện trong chuỗi được tìm kiếm, lần xuất hiện đầu tiên của văn bản phù hợp sẽ được trả về dưới dạng đối tượng Match. Nhập thông tin sau vào vỏ tương tác

    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    4
    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    5

    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    16
    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    7
    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    8
    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    9
    \d{3}-\d{3}-\d{4}
    0
    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    21
    \d{3}-\d{3}-\d{4}
    2

    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    23
    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    7
    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    25____126
    \d{3}-\d{3}-\d{4}
    2

    \d{3}-\d{3}-\d{4}
    8
    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    29

    ĐẦU RA

    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    1

    Kết hợp các lần lặp lại cụ thể với Dấu ngoặc nhọn

    Nếu bạn có một nhóm mà bạn muốn lặp lại một số lần cụ thể, hãy theo dõi nhóm trong biểu thức chính quy của bạn với một số trong dấu ngoặc nhọn. Ví dụ: biểu thức chính quy [Ha]{3} sẽ khớp với chuỗi 'HaHaHa', nhưng nó sẽ không khớp với 'HaHa', vì chuỗi sau chỉ có hai lần lặp lại của nhóm [Ha]

    Thay vì một số, bạn có thể chỉ định một phạm vi bằng cách viết giá trị tối thiểu, dấu phẩy và giá trị tối đa vào giữa các dấu ngoặc nhọn. Ví dụ: biểu thức chính quy [Ha]{3, 5} sẽ khớp với 'HaHaHa', 'HaHaHaHa' và 'HaHaHaHaHa'

    Bạn cũng có thể bỏ qua số thứ nhất hoặc số thứ hai trong dấu ngoặc nhọn để không giới hạn số tối thiểu hoặc tối đa. Ví dụ: [Ha]{3, } sẽ khớp với ba hoặc nhiều phiên bản của nhóm [Ha], trong khi [Ha]{, 5} sẽ khớp từ 0 đến năm phiên bản. Dấu ngoặc nhọn có thể giúp làm cho biểu thức chính quy của bạn ngắn hơn. Hai biểu thức chính quy này khớp với các mẫu giống hệt nhau

    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    2

    Và hai biểu thức chính quy này cũng khớp với các mẫu giống hệt nhau

    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    3

    Nhập thông tin sau vào vỏ tương tác

    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    4
    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    5

    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    32
    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    7
    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    8
    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    9
    \d{3}-\d{3}-\d{4}
    0
    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    37
    \d{3}-\d{3}-\d{4}
    2

    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    23
    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    7
    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    41
    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    42
    \d{3}-\d{3}-\d{4}
    2

    \d{3}-\d{3}-\d{4}
    8
    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    29

    ĐẦU RA

    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    4

    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    4
    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    5

    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    32
    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    7
    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    8
    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    9
    \d{3}-\d{3}-\d{4}
    0
    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    37
    \d{3}-\d{3}-\d{4}
    2

    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    55
    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    7
    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    41
    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    58
    \d{3}-\d{3}-\d{4}
    2
    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    7
    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    7
    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    62

    \d{3}-\d{3}-\d{4}
    8
    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    64

    ĐẦU RA

    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    5

    Ở đây, [Ha]{3} khớp với 'HaHaHa' chứ không khớp với 'Ha'. Vì nó không khớp với 'Ha', tìm kiếm [] trả về Không có

    Kết hợp tùy chọn với Dấu chấm hỏi

    Đôi khi có một mẫu mà bạn chỉ muốn khớp tùy chọn. Đó là, biểu thức chính quy sẽ tìm thấy kết quả phù hợp cho dù có hay không có đoạn văn bản đó. Các ? . Ví dụ: nhập thông tin sau vào trình bao tương tác

    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    4
    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    5

    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    67
    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    7
    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    8
    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    9
    \d{3}-\d{3}-\d{4}
    0
    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    72
    \d{3}-\d{3}-\d{4}
    2

    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    23
    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    7
    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    76
    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    77
    \d{3}-\d{3}-\d{4}
    2

    \d{3}-\d{3}-\d{4}
    8
    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    29

    ĐẦU RA

    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    1

    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    4
    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    5

    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    67
    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    7
    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    8
    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    9
    \d{3}-\d{3}-\d{4}
    0
    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    72
    \d{3}-\d{3}-\d{4}
    2

    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    55
    Any other string would not match the pattern.
    \d\d\d-\d\d\d-\d\d\d\d
    7
    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    76____193
    \d{3}-\d{3}-\d{4}
    2

    \d{3}-\d{3}-\d{4}
    8
    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    96

    ĐẦU RA

    if re.search[r"ID:{}".format[id], serial]
        print["ID match found"]
    else:
        print["ID match not found"]
    
    #output
    "ID match found"
    
    #For both ID and PR I've tried
    if re.search[r"ID:{}".format[id]."PR:{}".format[pr], serial]
        print["PR match found"]
    else:
        print["PR match not found"]
    
    #output
    "SyntaxError: invalid syntax" 
    
    7

    Cái [wo]? . Regex sẽ khớp với văn bản không có phiên bản nào hoặc có một phiên bản wo trong đó. Đây là lý do tại sao regex khớp với cả 'Batwoman' và 'Batman'.
    Bạn có thể nghĩ về dấu ? . ”
    Nếu bạn cần khớp một ký tự dấu cộng thực tế, hãy thêm dấu gạch chéo ngược vào trước dấu cộng để thoát ký tự đó. \+.

Chủ Đề