Python regex cho chữ và số và ký tự đặc biệt

Trong bài viết này, chúng ta sẽ xem cách sử dụng các chuỗi và lớp ký tự đặc biệt của regex trong Python. Trình tự đặc biệt của biểu thức chính quy Python đại diện cho một số ký tự đặc biệt để nâng cao khả năng của biểu thức chính quy

Show

Mục lục

Trình tự đặc biệt

Dãy đặc biệt đại diện cho các lớp ký tự cơ bản được xác định trước, có một ý nghĩa duy nhất. Mỗi trình tự đặc biệt làm cho các mẫu phổ biến cụ thể trở nên thoải mái hơn khi sử dụng

Ví dụ: bạn có thể sử dụng chuỗi

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
0 làm định nghĩa đơn giản hóa cho lớp ký tự
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
1, có nghĩa là khớp với bất kỳ chữ số nào từ 0 đến 9

Hãy xem danh sách các chuỗi đặc biệt của regex và ý nghĩa của chúng. Các chuỗi đặc biệt bao gồm 

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
2  (phản ứng dữ dội) và một ký tự từ bảng bên dưới

Chuỗi đặc biệt Ý nghĩa
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
3Chỉ khớp mẫu ở đầu chuỗi
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
4Chỉ khớp mẫu ở cuối chuỗi
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
0Chỉ khớp với bất kỳ chữ số nào.
Viết tắt của các lớp ký tự 
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
1
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
7Khớp với bất kỳ ký tự không có chữ số nào.
viết tắt của 
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
8
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
9Khớp với bất kỳ ký tự khoảng trắng nào.
viết tắt của lớp ký tự
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
00
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
01Khớp với mọi ký tự không phải khoảng trắng.
viết tắt của
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
02
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
03Khớp với bất kỳ ký tự chữ và số nào.
viết tắt của lớp ký tự
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
04
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
05Khớp với bất kỳ ký tự không phải chữ và số nào.
viết tắt của
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
06
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
07Khớp với chuỗi trống, nhưng chỉ ở đầu hoặc cuối từ. Khớp với một ranh giới từ trong đó một ký tự từ là
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
08.
Ví dụ: ‘
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
09 khớp với ‘Jessa’, ‘Jessa. ’, ‘(Jessa)’, ‘Jessa Emma Kelly’ chứ không phải ‘JessaKelly’ hay ‘Jessa5’. ________ 200 Đối lập với ________ 107. Khớp với chuỗi trống, nhưng chỉ khi nó không ở đầu hoặc cuối của biểu thức chính quy wordPython Chuỗi đặc biệt

lớp nhân vật

Trong Python, các lớp ký tự regex là tập hợp các ký tự hoặc phạm vi ký tự được đặt trong dấu ngoặc vuông

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
02

Ví dụ:

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
03 nó có nghĩa là khớp với bất kỳ chữ cái viết thường nào từ a đến z.  

Hãy xem một số lớp ký tự phổ biến nhất được sử dụng bên trong các mẫu biểu thức chính quy

Lớp ký tự Mô tả
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
04Ghép chữ cái a hoặc b hoặc c
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
05Ghép chữ cái a hoặc b hoặc c theo sau bởi p hoặc q.
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
06Ghép bất kỳ chữ cái nào ngoại trừ a, b hoặc c (phủ định)
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
1Ghép bất kỳ chữ số nào từ 0 đến 9. bao gồm (phạm vi)
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
03Ghép bất kỳ chữ cái viết thường nào từ a đến z. bao gồm (phạm vi)
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
09Ghép bất kỳ chữ cái HOA nào từ A đến Z. bao gồm (phạm vi)
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
00Khớp với bất kỳ chữ thường hoặc chữ HOA nào. bao gồm (phạm vi) ______301Phạm vi. khớp với một chữ cái giữa m và p và các chữ số từ 2 đến 8, nhưng không phải p2
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
08Khớp với bất kỳ ký tự chữ và số nào Lớp ký tự Python regex

Bây giờ, hãy xem cách sử dụng từng lớp ký tự và chuỗi đặc biệt trong biểu thức chính quy Python

Trình tự đặc biệt import re target_str = "8000 dollar" # \d to match all digits result = re.findall(r"\d", target_str) print(result) # Output ['8', '0', '0', '0'] # \d to match all numbers result = re.findall(r"\d+", target_str) print(result) # Output ['8000'] # \D to match non-digits result = re.findall(r"\D", target_str) print(result) # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']3 và import re target_str = "8000 dollar" # \d to match all digits result = re.findall(r"\d", target_str) print(result) # Output ['8', '0', '0', '0'] # \d to match all numbers result = re.findall(r"\d+", target_str) print(result) # Output ['8000'] # \D to match non-digits result = re.findall(r"\D", target_str) print(result) # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']4

Dấu gạch chéo ngược A (

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
3 )

Các chuỗi

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
3 chỉ khớp với phần đầu của chuỗi. Nó hoạt động giống như siêu ký tự dấu mũ (_______307)

Mặt khác, nếu chúng ta có một chuỗi nhiều dòng, thì

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
3 sẽ vẫn chỉ khớp ở đầu chuỗi, trong khi dấu mũ sẽ khớp ở đầu mỗi dòng mới của chuỗi

Các chuỗi dấu gạch chéo ngược Z (

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
4 ) chỉ khớp với phần cuối của chuỗi. Nó hoạt động giống như siêu ký tự đô la ($)

Thí dụ

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
0

Chuỗi đặc biệt import re target_str = "8000 dollar" # \d to match all digits result = re.findall(r"\d", target_str) print(result) # Output ['8', '0', '0', '0'] # \d to match all numbers result = re.findall(r"\d+", target_str) print(result) # Output ['8000'] # \D to match non-digits result = re.findall(r"\D", target_str) print(result) # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']0 và import re target_str = "8000 dollar" # \d to match all digits result = re.findall(r"\d", target_str) print(result) # Output ['8', '0', '0', '0'] # \d to match all numbers result = re.findall(r"\d+", target_str) print(result) # Output ['8000'] # \D to match non-digits result = re.findall(r"\D", target_str) print(result) # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']7

Dấu gạch chéo ngược d (

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
0 )

  • import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    0 khớp với bất kỳ chữ số nào từ 0 đến 9 bên trong chuỗi mục tiêu
  • Dãy số đặc biệt này tương đương với lớp ký tự
    import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    1
  • Sử dụng
    import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    0 hoặc
    import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    1

Dấu gạch chéo ngược viết hoa D (

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
7 )

  • Chuỗi này hoàn toàn ngược lại với
    import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    0 và nó khớp với bất kỳ ký tự không phải chữ số nào
  • Bất kỳ ký tự nào trong chuỗi đích không phải là chữ số sẽ tương đương với ký tự
    import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    7
  • Ngoài ra, bạn có thể viết
    import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    7 bằng cách sử dụng lớp ký tự
    import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    8  (dấu mũ ^ ở đầu lớp ký tự biểu thị phủ định)

Thí dụ

Bây giờ hãy làm như sau

  1. Sử dụng chuỗi đặc biệt
    import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    0 bên trong mẫu biểu thức chính quy để tìm số có 4 chữ số trong chuỗi mục tiêu của chúng tôi
  2. Sử dụng một chuỗi đặc biệt
    import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    7 bên trong mẫu biểu thức chính quy để tìm tất cả các ký tự không phải chữ số
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']

Chuỗi đặc biệt import re target_str = "8000 dollar" # \d to match all digits result = re.findall(r"\d", target_str) print(result) # Output ['8', '0', '0', '0'] # \d to match all numbers result = re.findall(r"\d+", target_str) print(result) # Output ['8000'] # \D to match non-digits result = re.findall(r"\D", target_str) print(result) # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']03 và import re target_str = "8000 dollar" # \d to match all digits result = re.findall(r"\d", target_str) print(result) # Output ['8', '0', '0', '0'] # \d to match all numbers result = re.findall(r"\d+", target_str) print(result) # Output ['8000'] # \D to match non-digits result = re.findall(r"\D", target_str) print(result) # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']05

Dấu gạch chéo ngược w (

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
03 )

  • import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    03 khớp với bất kỳ ký tự chữ và số nào, còn được gọi là ký tự từ
  • Điều này bao gồm chữ thường và chữ in hoa, các chữ số từ 0 đến 9 và ký tự gạch dưới
  • Tương đương với lớp nhân vật
    import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    08
  • Bạn có thể sử dụng
    import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    03 hoặc
    import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    08

Dấu gạch chéo ngược viết hoa W (

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
05 )

  • Trình tự này hoàn toàn ngược lại với
    import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    03, tôi. e. , Nó khớp với bất kỳ ký tự KHÔNG chữ và số nào
  • Bất kỳ ký tự nào trong chuỗi đích không phải là chữ và số sẽ tương đương với ký tự
    import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    05
  • Bạn có thể viết
    import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    05 bằng cách sử dụng lớp ký tự
    import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    75

Thí dụ

Bây giờ hãy làm như sau

  1. Sử dụng chuỗi đặc biệt
    import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    03 bên trong mẫu biểu thức chính quy để tìm tất cả ký tự chữ và số trong chuỗi
  2. Sử dụng một chuỗi đặc biệt
    import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    05 bên trong mẫu biểu thức chính quy để tìm tất cả các ký tự không phải chữ và số
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
0

Chuỗi đặc biệt import re target_str = "8000 dollar" # \d to match all digits result = re.findall(r"\d", target_str) print(result) # Output ['8', '0', '0', '0'] # \d to match all numbers result = re.findall(r"\d+", target_str) print(result) # Output ['8000'] # \D to match non-digits result = re.findall(r"\D", target_str) print(result) # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']9 và import re target_str = "8000 dollar" # \d to match all digits result = re.findall(r"\d", target_str) print(result) # Output ['8', '0', '0', '0'] # \d to match all numbers result = re.findall(r"\d+", target_str) print(result) # Output ['8000'] # \D to match non-digits result = re.findall(r"\D", target_str) print(result) # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']01

Dấu gạch chéo ngược chữ thường s (

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
9 )

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
9 khớp với bất kỳ ký tự khoảng trắng nào bên trong chuỗi đích. Các ký tự khoảng trắng được bao phủ bởi chuỗi này như sau

  • không gian chung được tạo bởi phím cách từ bàn phím. (
    import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    52)
  • Ký tự tab (______753)
  • Ký tự xuống dòng (
    import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    54)
  • Vận chuyển trở lại (
    import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    55)
  • nguồn cấp dữ liệu biểu mẫu (______756)
  • Tab dọc (
    import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    57)

Ngoài ra, dãy đặc biệt này tương đương với lớp ký tự

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
00. Vì vậy, bạn có thể sử dụng
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
9 hoặc
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
00

Dấu gạch chéo ngược vốn S (

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
01 )

Trình tự này hoàn toàn ngược lại với

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
9 và nó khớp với bất kỳ ký tự KHÔNG phải khoảng trắng nào. Bất kỳ ký tự nào trong chuỗi đích không phải là khoảng trắng sẽ tương đương với ký tự
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
01

Ngoài ra, bạn có thể viết

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
01 sử dụng lớp ký tự
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
02

Thí dụ

Bây giờ hãy làm như sau

  1. Sử dụng chuỗi đặc biệt
    import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    9 bên trong mẫu biểu thức chính quy để tìm tất cả ký tự khoảng trắng trong chuỗi mục tiêu của chúng tôi
  2. Sử dụng chuỗi đặc biệt
    import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    01 bên trong mẫu biểu thức chính quy để tìm tất cả ký tự KHÔNG phải khoảng trắng
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
0

Chuỗi đặc biệt import re target_str = "8000 dollar" # \d to match all digits result = re.findall(r"\d", target_str) print(result) # Output ['8', '0', '0', '0'] # \d to match all numbers result = re.findall(r"\d+", target_str) print(result) # Output ['8000'] # \D to match non-digits result = re.findall(r"\D", target_str) print(result) # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']07 và import re target_str = "8000 dollar" # \d to match all digits result = re.findall(r"\d", target_str) print(result) # Output ['8', '0', '0', '0'] # \d to match all numbers result = re.findall(r"\d+", target_str) print(result) # Output ['8000'] # \D to match non-digits result = re.findall(r"\D", target_str) print(result) # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']00

Dấu gạch chéo ngược chữ thường b (

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
07 )

Chuỗi đặc biệt

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
07 khớp với các chuỗi trống giáp với từ. Dấu gạch chéo ngược
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
07 được sử dụng trong các mẫu biểu thức chính quy để báo hiệu các ranh giới của từ, hay nói cách khác, các đường viền hoặc cạnh của một từ

Ghi chú. Một từ là một tập hợp các ký tự chữ và số được bao quanh bởi các ký tự không phải chữ và số (chẳng hạn như dấu cách)

Thí dụ

Hãy thử ghép tất cả các từ có 6 chữ cái bằng cách sử dụng một chuỗi đặc biệt

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
03 và 
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
07

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
0

Ghi chú.  

Một điều quan trọng cần lưu ý ở đây là từ khớp sẽ chỉ được thực hiện cho chính từ hoàn chỉnh và riêng biệt đó. Sẽ không có trận đấu nào được trả lại nếu từ được chứa bên trong một từ khác

Chẳng hạn, xem xét cùng một chuỗi mục tiêu, chúng ta có thể tìm kiếm từ “ssa” bằng cách sử dụng chuỗi đặc biệt

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
07 như thế này
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
06. Nhưng chúng tôi sẽ không khớp vì các ký tự không phải chữ và số không viền nó ở cả hai bên

Hơn nữa, chuỗi

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
07 luôn khớp với chuỗi trống hoặc ranh giới giữa ký tự chữ và số và ký tự không phải chữ và số

Do đó, hãy nhớ rằng từ bạn đang cố gắng ghép với sự trợ giúp của chuỗi đặc biệt

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
07 phải tách biệt, không phải là một phần của từ

Dấu gạch chéo ngược viết hoa B (

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
00 )

Trình tự này hoàn toàn ngược lại với

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
07

Mặt khác, dãy đặc biệt

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
00 khớp với chuỗi trống hoặc đường viền giữa hai ký tự chữ và số hoặc hai ký tự không phải chữ và số chỉ khi nó không ở đầu hoặc ở cuối một từ

Vì vậy, trình tự này có thể hữu ích để khớp và định vị một số chuỗi trong một từ cụ thể

Ví dụ: hãy sử dụng

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
00 để kiểm tra xem chuỗi 'thon' có nằm trong chuỗi mục tiêu mà không phải ở đầu từ hay không. Vì vậy, 'thon' phải là một phần của từ lớn hơn trong chuỗi của chúng tôi, nhưng không phải ở đầu từ

Thí dụ

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
08

Và thực sự, chúng tôi có một kết quả khớp của

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
13 bên trong từ “Python” không nằm ở đầu từ. Điều gì sẽ xảy ra nếu chúng ta muốn kiểm tra xem
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
13 có phải là một phần của từ trong chuỗi đích nhưng không ở cuối từ đó không

Chà, chúng ta phải di chuyển chuỗi

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
00 ở cuối mẫu. Hãy cũng thử điều này

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
0

Tạo các lớp ký tự tùy chỉnh

Chúng ta có thể xây dựng các lớp ký tự bằng các cách sau

  1. lớp học đơn giản
  2. phủ định
  3. các dãy

Các lớp nhân vật đơn giản

Hình thức cơ bản nhất của một lớp ký tự là đặt một tập hợp các ký tự cạnh nhau trong dấu ngoặc vuông

Ví dụ: biểu thức chính quy

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
16 sẽ khớp với các từ “pot”, “hot” hoặc “lot” vì nó định nghĩa một lớp ký tự chấp nhận 'p', 'h' hoặc 'l' làm ký tự đầu tiên theo sau là '

Hãy xem ví dụ Python về cách sử dụng các lớp ký tự đơn giản trong mẫu biểu thức chính quy

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
7

Sử dụng phủ định để xây dựng các lớp ký tự

Để khớp với tất cả các ký tự ngoại trừ những ký tự được liệt kê bên trong dấu ngoặc vuông, hãy chèn ký tự phụ

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
17 vào đầu lớp ký tự. Kỹ thuật này được gọi là phủ định

  1. import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    06 khớp với bất kỳ ký tự nào ngoại trừ a, b hoặc c
  2. import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    8 khớp với bất kỳ ký tự nào ngoại trừ chữ số

Thí dụ

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
5

Sử dụng phạm vi để xây dựng các lớp ký tự

Đôi khi, bạn sẽ muốn xác định một lớp ký tự bao gồm một loạt các giá trị, chẳng hạn như các chữ cái “m đến p” hoặc các số “2 đến 6“. Để chỉ định một phạm vi, chỉ cần chèn ký tự phụ

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
20 vào giữa ký tự đầu tiên và ký tự cuối cùng được khớp, chẳng hạn như
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
21 hoặc
import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
22

Hãy xem cách sử dụng phạm vi để xây dựng các lớp ký tự regex

  • import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    03 khớp với bất kỳ chữ cái viết thường nào từ a đến z
  • import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    09 khớp với bất kỳ chữ cái HOA nào từ A đến Z
  • import re
    
    target_str = "8000 dollar"
    
    # \d to match all digits
    result = re.findall(r"\d", target_str)
    print(result)
    # Output ['8', '0', '0', '0']
    
    # \d to match all numbers
    result = re.findall(r"\d+", target_str)
    print(result)
    # Output ['8000']
    
    # \D to match non-digits
    result = re.findall(r"\D", target_str)
    print(result)
    # Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
    22 khớp với bất kỳ chữ số nào từ 2 đến 6

Bạn cũng có thể đặt các phạm vi khác nhau cạnh nhau trong lớp để tăng thêm khả năng khớp

Ví dụ:

import re

target_str = "8000 dollar"

# \d to match all digits
result = re.findall(r"\d", target_str)
print(result)
# Output ['8', '0', '0', '0']

# \d to match all numbers
result = re.findall(r"\d+", target_str)
print(result)
# Output ['8000']

# \D to match non-digits
result = re.findall(r"\D", target_str)
print(result)
# Output [' ', 'd', 'o', 'l', 'l', 'a', 'r']
26 sẽ khớp với bất kỳ chữ cái nào trong bảng chữ cái. a đến z (chữ thường) hoặc A đến Z (chữ hoa)

Làm cách nào để kiểm tra chuỗi là chữ và số và ký tự đặc biệt trong Python?

Để giải quyết vấn đề này Hàm chuỗi Python isalnum() được sử dụng. Hàm Chuỗi isalnum() của Python kiểm tra một chuỗi để tìm các ký tự chữ và số và chỉ trả về True nếu chuỗi đó chứa các ký tự chữ và số, i. e. bảng chữ cái (a-z, A-Z) hoặc chữ số (0-9) hoặc kết hợp cả hai.

Chữ và số có cho phép các ký tự đặc biệt không?

Mật khẩu chữ và số chứa số, chữ cái và ký tự đặc biệt (như dấu và hoặc dấu thăng).

'$' nghĩa là gì trong regex?

*$ có nghĩa là - khớp, từ đầu đến cuối, bất kỳ ký tự nào xuất hiện từ 0 lần trở lên . Về cơ bản, điều đó có nghĩa là - khớp mọi thứ từ đầu đến cuối chuỗi.

Regex \W có bao gồm các chữ số không?

\w là viết tắt của “ký tự từ”. Nó luôn khớp với các ký tự ASCII [A-Za-z0-9_]. Lưu ý việc bao gồm dấu gạch dưới và chữ số . Trong hầu hết các hương vị hỗ trợ Unicode, \w bao gồm nhiều ký tự từ các tập lệnh khác.