Nên sử dụng mô -đun PYPI
result = "".join[re.findall[r'[^\W\d_]', text]]
2 nếu bạn có kế hoạch khớp các lớp thuộc tính Unicode cụ thể. Thư viện này cũng đã được chứng minh là ổn định hơn, đặc biệt là xử lý các văn bản lớn và mang lại kết quả nhất quán trên các phiên bản Python khác nhau. Tất cả những gì bạn cần làm là giữ cho nó cập nhật.Nếu bạn cài đặt nó [sử dụng
result = "".join[re.findall[r'[^\W\d_]', text]]
3 hoặc result = "".join[re.findall[r'[^\W\d_]', text]]
4], bạn có thể sử dụngimport regex
print [ regex.sub[r'\P{L}+', '', 'ABCŁąć1-2!Абв3§4“5def”'] ]
// => ABCŁąćАбвdef
Để loại bỏ tất cả các khối của 1 hoặc nhiều ký tự khác ngoài các chữ cái Unicode từ
result = "".join[re.findall[r'[^\W\d_]', text]]
5. Xem bản demo Python trực tuyến. Bạn cũng có thể sử dụng result = "".join[re.findall[r'[^\W\d_]', text]]
6 để có được kết quả tương tự.Trong Python
result = "".join[re.findall[r'[^\W\d_]', text]]
7, để phù hợp với bất kỳ chữ cái Unicode nào, người ta có thể sử dụng cấu trúc result = "".join[re.findall[r'[^\W\d_]', text]]
8 [khớp với bất kỳ chữ cái unicode nào?].Vì vậy, để xóa tất cả các ký tự không chữ, bạn có thể khớp với tất cả các chữ cái và tham gia kết quả:
result = "".join[re.findall[r'[^\W\d_]', text]]
Hoặc, xóa tất cả các ký tự phù hợp với mẫu
result = "".join[re.findall[r'[^\W\d_]', text]]
9 [ngược lại với result = "".join[re.findall[r'[^\W\d_]', text]]
8]:result = re.sub[r'[\W\d_]+', '', text]
Xem bản demo regex trực tuyến. Tuy nhiên, bạn có thể nhận được kết quả không nhất quán trên các phiên bản Python khác nhau vì tiêu chuẩn Unicode đang phát triển và tập hợp các ký tự phù hợp với
result = re.sub[r'[\W\d_]+', '', text]
1 sẽ phụ thuộc vào phiên bản Python. Sử dụng thư viện PYPI result = "".join[re.findall[r'[^\W\d_]', text]]
2 rất được khuyến khích để có kết quả nhất quán.However, you may get inconsistent results across various
Python versions because the Unicode standard is evolving, and the set of chars matched with result = re.sub[r'[\W\d_]+', '', text]
1 will depend on the Python version. Using PyPi result = "".join[re.findall[r'[^\W\d_]', text]]
2 library is highly recommended to get consistent results. Xóa tất cả các ký tự không alphabetic khỏi chuỗi trong Python #
Sử dụng phương thức
3 để loại bỏ tất cả các ký tự không alphabetic khỏi chuỗi, ví dụ: result = re.sub[r'[\W\d_]+', '', text]
4. Phương pháp result = re.sub[r'[\W\d_]+', '', text]
3 sẽ loại bỏ tất cả các ký tự không alphabetic khỏi chuỗi bằng cách thay thế chúng bằng các chuỗi trống.result = re.sub[r'[\W\d_]+', '', text]
Copied!
import re my_str = 'one! two@ three' # ✅ Remove all non-alphabetic characters from string [re.sub[]] new_str = re.sub[r'[^a-zA-Z]', '', my_str] print[new_str] # 👉️ 'onetwothree' # ✅ Remove all non-alphabetic characters from string, preserving whitespace new_str = re.sub[r'[^a-zA-Z\s]', '', my_str] print[new_str] # 👉️ 'one two three' # ---------------------------------------------------------------- # ✅ Remove all non-alphabetic characters from string [generator expression] new_str = ''.join[char for char in my_str if char.isalpha[]] print[new_str] # 👉️ 'onetwothree' # ✅ Preserve whitespace new_str = ''.join[char for char in my_str if char.isalpha[] or char == ' '] print[new_str] # 👉️ 'one two three'
Ví dụ đầu tiên sử dụng phương thức
result = re.sub[r'[\W\d_]+', '', text]
3 để loại bỏ tất cả các ký tự không alphabetic khỏi chuỗi.Phương thức Re.sub trả về một chuỗi mới thu được bằng cách thay thế các lần xuất hiện của mẫu bằng thay thế được cung cấp.
Copied!
import re my_str = 'one! two@ three' new_str = re.sub[r'[^a-zA-Z]', '', my_str] print[new_str] # 👉️ 'onetwothree' new_str = re.sub[r'[^a-zA-Z\s]', '', my_str] print[new_str] # 👉️ 'one two three'
Nếu mẫu không được tìm thấy, chuỗi được trả về như vậy.
Đối số đầu tiên chúng tôi đã chuyển sang phương pháp
result = re.sub[r'[\W\d_]+', '', text]
3 là một biểu thức chính quy.Các dấu ngoặc vuông
result = re.sub[r'[\W\d_]+', '', text]
8 được sử dụng để chỉ ra một tập hợp các ký tự.CARET
9 ở đầu tập hợp có nghĩa là "không". Nói cách khác, khớp với tất cả các ký tự không phải là chữ cái.result = re.sub[r'[\W\d_]+', '', text]
Các ký tự
0 vàCopied!
import re my_str = 'one! two@ three' # ✅ Remove all non-alphabetic characters from string [re.sub[]] new_str = re.sub[r'[^a-zA-Z]', '', my_str] print[new_str] # 👉️ 'onetwothree' # ✅ Remove all non-alphabetic characters from string, preserving whitespace new_str = re.sub[r'[^a-zA-Z\s]', '', my_str] print[new_str] # 👉️ 'one two three' # ---------------------------------------------------------------- # ✅ Remove all non-alphabetic characters from string [generator expression] new_str = ''.join[char for char in my_str if char.isalpha[]] print[new_str] # 👉️ 'onetwothree' # ✅ Preserve whitespace new_str = ''.join[char for char in my_str if char.isalpha[] or char == ' '] print[new_str] # 👉️ 'one two three'
1 đại diện cho phạm vi chữ cái chữ thường và chữ hoa.Copied!
import re my_str = 'one! two@ three' # ✅ Remove all non-alphabetic characters from string [re.sub[]] new_str = re.sub[r'[^a-zA-Z]', '', my_str] print[new_str] # 👉️ 'onetwothree' # ✅ Remove all non-alphabetic characters from string, preserving whitespace new_str = re.sub[r'[^a-zA-Z\s]', '', my_str] print[new_str] # 👉️ 'one two three' # ---------------------------------------------------------------- # ✅ Remove all non-alphabetic characters from string [generator expression] new_str = ''.join[char for char in my_str if char.isalpha[]] print[new_str] # 👉️ 'onetwothree' # ✅ Preserve whitespace new_str = ''.join[char for char in my_str if char.isalpha[] or char == ' '] print[new_str] # 👉️ 'one two three'
Nếu bạn cần loại bỏ tất cả các ký tự không alphabetic và bảo quản khoảng trắng, hãy sử dụng biểu thức chính quy sau.
Copied!
import re my_str = 'one! two@ three' new_str = re.sub[r'[^a-zA-Z\s]', '', my_str] print[new_str] # 👉️ 'one two three'
Nhân vật
2 phù hợp với các ký tự khoảng trắng unicode nhưCopied!
import re my_str = 'one! two@ three' # ✅ Remove all non-alphabetic characters from string [re.sub[]] new_str = re.sub[r'[^a-zA-Z]', '', my_str] print[new_str] # 👉️ 'onetwothree' # ✅ Remove all non-alphabetic characters from string, preserving whitespace new_str = re.sub[r'[^a-zA-Z\s]', '', my_str] print[new_str] # 👉️ 'one two three' # ---------------------------------------------------------------- # ✅ Remove all non-alphabetic characters from string [generator expression] new_str = ''.join[char for char in my_str if char.isalpha[]] print[new_str] # 👉️ 'onetwothree' # ✅ Preserve whitespace new_str = ''.join[char for char in my_str if char.isalpha[] or char == ' '] print[new_str] # 👉️ 'one two three'
3.Copied!
import re my_str = 'one! two@ three' # ✅ Remove all non-alphabetic characters from string [re.sub[]] new_str = re.sub[r'[^a-zA-Z]', '', my_str] print[new_str] # 👉️ 'onetwothree' # ✅ Remove all non-alphabetic characters from string, preserving whitespace new_str = re.sub[r'[^a-zA-Z\s]', '', my_str] print[new_str] # 👉️ 'one two three' # ---------------------------------------------------------------- # ✅ Remove all non-alphabetic characters from string [generator expression] new_str = ''.join[char for char in my_str if char.isalpha[]] print[new_str] # 👉️ 'onetwothree' # ✅ Preserve whitespace new_str = ''.join[char for char in my_str if char.isalpha[] or char == ' '] print[new_str] # 👉️ 'one two three'
Toàn bộ, biểu thức chính quy phù hợp với tất cả các ký tự không có chữ hoặc khoảng trắng.
Nếu bạn cần trợ giúp đọc hoặc viết một biểu thức chính quy, hãy tham khảo ý kiến phân nhóm cú pháp biểu thức thông thường trong các tài liệu chính thức.
Trang chứa một danh sách tất cả các ký tự đặc biệt với nhiều ví dụ hữu ích.
Nếu chuỗi của bạn có nhiều khoảng trống cạnh nhau, bạn có thể phải thay thế nhiều không gian liên tiếp bằng một không gian.
Copied!
import re my_str = 'one! two@ three' new_str = re.sub[r'[^a-zA-Z\s]', '', my_str] print[new_str] # 👉️ 'one two three' result = ' '.join[new_str.split[]] print[result] # 👉️ 'one two three'
Phương thức Copied!
import re
my_str = 'one! two@ three'
# ✅ Remove all non-alphabetic characters from string [re.sub[]]
new_str = re.sub[r'[^a-zA-Z]', '', my_str]
print[new_str] # 👉️ 'onetwothree'
# ✅ Remove all non-alphabetic characters from string, preserving whitespace
new_str = re.sub[r'[^a-zA-Z\s]', '', my_str]
print[new_str] # 👉️ 'one two three'
# ----------------------------------------------------------------
# ✅ Remove all non-alphabetic characters from string [generator expression]
new_str = ''.join[char for char in my_str if char.isalpha[]]
print[new_str] # 👉️ 'onetwothree'
# ✅ Preserve whitespace
new_str = ''.join[char for char in my_str if char.isalpha[] or char == ' ']
print[new_str] # 👉️ 'one two three'
4 chia chuỗi trên một hoặc nhiều ký tự khoảng trắng và chúng tôi tham gia danh sách các chuỗi với một bộ phân cách không gian duy nhất.Ngoài ra, bạn có thể sử dụng một biểu thức máy phát.
Để loại bỏ tất cả các ký tự không alphabetic khỏi chuỗi:
- Sử dụng biểu thức máy phát để lặp qua chuỗi.
- Sử dụng phương pháp
5 để kiểm tra xem mỗi ký tự là chữ cái.Copied!
import re my_str = 'one! two@ three' # ✅ Remove all non-alphabetic characters from string [re.sub[]] new_str = re.sub[r'[^a-zA-Z]', '', my_str] print[new_str] # 👉️ 'onetwothree' # ✅ Remove all non-alphabetic characters from string, preserving whitespace new_str = re.sub[r'[^a-zA-Z\s]', '', my_str] print[new_str] # 👉️ 'one two three' # ---------------------------------------------------------------- # ✅ Remove all non-alphabetic characters from string [generator expression] new_str = ''.join[char for char in my_str if char.isalpha[]] print[new_str] # 👉️ 'onetwothree' # ✅ Preserve whitespace new_str = ''.join[char for char in my_str if char.isalpha[] or char == ' '] print[new_str] # 👉️ 'one two three'
- Sử dụng phương thức
6 để tham gia các ký tự chữ cái.Copied!
import re my_str = 'one! two@ three' # ✅ Remove all non-alphabetic characters from string [re.sub[]] new_str = re.sub[r'[^a-zA-Z]', '', my_str] print[new_str] # 👉️ 'onetwothree' # ✅ Remove all non-alphabetic characters from string, preserving whitespace new_str = re.sub[r'[^a-zA-Z\s]', '', my_str] print[new_str] # 👉️ 'one two three' # ---------------------------------------------------------------- # ✅ Remove all non-alphabetic characters from string [generator expression] new_str = ''.join[char for char in my_str if char.isalpha[]] print[new_str] # 👉️ 'onetwothree' # ✅ Preserve whitespace new_str = ''.join[char for char in my_str if char.isalpha[] or char == ' '] print[new_str] # 👉️ 'one two three'
Copied!
my_str = 'one! two@ three' new_str = ''.join[char for char in my_str if char.isalpha[]] print[new_str] # 👉️ 'onetwothree' new_str = ''.join[char for char in my_str if char.isalpha[] or char == ' '] print[new_str] # 👉️ 'one two three'
Chúng tôi đã sử dụng một biểu thức máy phát để lặp qua chuỗi.
Biểu thức của máy phát được sử dụng để thực hiện một số hoạt động cho mọi phần tử hoặc chọn một tập hợp con của các phần tử đáp ứng một điều kiện.
Trên mỗi lần lặp, chúng tôi sử dụng phương pháp
5 để kiểm tra xem ký tự hiện tại có chữ cái không và chúng tôi trả về kết quả.Copied!
import re my_str = 'one! two@ three' # ✅ Remove all non-alphabetic characters from string [re.sub[]] new_str = re.sub[r'[^a-zA-Z]', '', my_str] print[new_str] # 👉️ 'onetwothree' # ✅ Remove all non-alphabetic characters from string, preserving whitespace new_str = re.sub[r'[^a-zA-Z\s]', '', my_str] print[new_str] # 👉️ 'one two three' # ---------------------------------------------------------------- # ✅ Remove all non-alphabetic characters from string [generator expression] new_str = ''.join[char for char in my_str if char.isalpha[]] print[new_str] # 👉️ 'onetwothree' # ✅ Preserve whitespace new_str = ''.join[char for char in my_str if char.isalpha[] or char == ' '] print[new_str] # 👉️ 'one two three'
Phương thức str.isalpha trả về
8 nếu tất cả các ký tự trong chuỗi là bảng chữ cái và có ít nhất một ký tự, nếu không phương thức trả vềCopied!
import re my_str = 'one! two@ three' # ✅ Remove all non-alphabetic characters from string [re.sub[]] new_str = re.sub[r'[^a-zA-Z]', '', my_str] print[new_str] # 👉️ 'onetwothree' # ✅ Remove all non-alphabetic characters from string, preserving whitespace new_str = re.sub[r'[^a-zA-Z\s]', '', my_str] print[new_str] # 👉️ 'one two three' # ---------------------------------------------------------------- # ✅ Remove all non-alphabetic characters from string [generator expression] new_str = ''.join[char for char in my_str if char.isalpha[]] print[new_str] # 👉️ 'onetwothree' # ✅ Preserve whitespace new_str = ''.join[char for char in my_str if char.isalpha[] or char == ' '] print[new_str] # 👉️ 'one two three'
9.Copied!
import re my_str = 'one! two@ three' # ✅ Remove all non-alphabetic characters from string [re.sub[]] new_str = re.sub[r'[^a-zA-Z]', '', my_str] print[new_str] # 👉️ 'onetwothree' # ✅ Remove all non-alphabetic characters from string, preserving whitespace new_str = re.sub[r'[^a-zA-Z\s]', '', my_str] print[new_str] # 👉️ 'one two three' # ---------------------------------------------------------------- # ✅ Remove all non-alphabetic characters from string [generator expression] new_str = ''.join[char for char in my_str if char.isalpha[]] print[new_str] # 👉️ 'onetwothree' # ✅ Preserve whitespace new_str = ''.join[char for char in my_str if char.isalpha[] or char == ' '] print[new_str] # 👉️ 'one two three'
Copied!
print['H'.isalpha[]] # 👉️ True print['@'.isalpha[]] # 👉️ False
Đối tượng Trình tạo chỉ chứa các ký tự chữ cái.
Copied!
my_str = 'one! two@ three' new_str = ''.join[char for char in my_str if char.isalpha[]] print[new_str] # 👉️ 'onetwothree'
Bước cuối cùng là sử dụng phương thức
6 để tham gia các ký tự chữ cái vào một chuỗi.Copied!
import re my_str = 'one! two@ three' # ✅ Remove all non-alphabetic characters from string [re.sub[]] new_str = re.sub[r'[^a-zA-Z]', '', my_str] print[new_str] # 👉️ 'onetwothree' # ✅ Remove all non-alphabetic characters from string, preserving whitespace new_str = re.sub[r'[^a-zA-Z\s]', '', my_str] print[new_str] # 👉️ 'one two three' # ---------------------------------------------------------------- # ✅ Remove all non-alphabetic characters from string [generator expression] new_str = ''.join[char for char in my_str if char.isalpha[]] print[new_str] # 👉️ 'onetwothree' # ✅ Preserve whitespace new_str = ''.join[char for char in my_str if char.isalpha[] or char == ' '] print[new_str] # 👉️ 'one two three'
Phương thức str.join lấy một điều đáng tin cậy như một đối số và trả về một chuỗi là sự kết hợp của các chuỗi trong điều kiện có thể sử dụng được.
Chuỗi phương thức được gọi là bật được sử dụng làm phân tách giữa các phần tử.
Đối với mục đích của chúng tôi, chúng tôi gọi phương thức Copied!
1 trên một chuỗi trống để tham gia các ký tự chữ cái mà không có dấu phân cách.import re
my_str = 'one! two@ three'
new_str = re.sub[r'[^a-zA-Z]', '', my_str]
print[new_str] # 👉️ 'onetwothree'
new_str = re.sub[r'[^a-zA-Z\s]', '', my_str]
print[new_str] # 👉️ 'one two three'
Nếu bạn muốn loại bỏ các ký tự không alphabetic và bảo quản khoảng trắng, hãy sử dụng toán tử boolean
2.Copied!
import re my_str = 'one! two@ three' new_str = re.sub[r'[^a-zA-Z]', '', my_str] print[new_str] # 👉️ 'onetwothree' new_str = re.sub[r'[^a-zA-Z\s]', '', my_str] print[new_str] # 👉️ 'one two three'
result = "".join[re.findall[r'[^\W\d_]', text]]
0Chúng tôi đã sử dụng toán tử boolean
2, vì vậy để ký tự được thêm vào đối tượng máy phát, một trong các điều kiện phải được đáp ứng.Copied!
import re my_str = 'one! two@ three' new_str = re.sub[r'[^a-zA-Z]', '', my_str] print[new_str] # 👉️ 'onetwothree' new_str = re.sub[r'[^a-zA-Z\s]', '', my_str] print[new_str] # 👉️ 'one two three'
Nhân vật phải là chữ cái hoặc nó phải là một không gian.
Nếu chuỗi của bạn chứa nhiều khoảng trống bên cạnh nhau, bạn có thể phải thay thế một hoặc nhiều khoảng trống bằng một khoảng trống.
result = "".join[re.findall[r'[^\W\d_]', text]]
1