Chia một chuỗi trên tất cả các ký tự đặc biệt trong Python #
Sử dụng phương thức re.split[]
để phân chia một chuỗi trên tất cả các ký tự đặc biệt. Phương thức re.split[]
lấy một mẫu và một chuỗi và phân tách chuỗi trên mỗi lần xuất hiện của mẫu.
Copied!
import re my_str = "hellothree.four!five'six" my_list = re.split[r'[`!@#$%^&*[]_+\-=\[\]{};\':"\\|,.\/?~]', my_str] # 👇️ ['hello', 'one', 'two', 'three', 'four', 'five', 'six'] print[my_list]
Chúng tôi đã sử dụng phương thức Re.Split để phân chia một chuỗi trên tất cả các lần xuất hiện của một ký tự đặc biệt.
Các dấu ngoặc vuông được sử dụng để chỉ ra một tập hợp các ký tự.
Hãy chắc chắn rằng tất cả các ký tự bạn coi là các ký tự đặc biệt đều nằm trong bộ.
Bạn có thể thêm hoặc xóa các ký tự theo trường hợp sử dụng của bạn.
Ngoài ra, bạn có thể sử dụng một biểu thức thông thường phù hợp với bất kỳ ký tự nào không phải là chữ cái, chữ số hoặc không gian.
Copied!
import re my_str = "hellothree.four!five'six" my_list = re.split[r'[^a-zA-Z0-9\s]', my_str] # 👇️ ['hello', 'one', 'two', 'three', 'four', 'five', 'six'] print[my_list]
Caret
0 ở đầ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ữ thườngCopied!
import re my_str = "hellothree.four!five'six" my_list = re.split[r'[^a-zA-Z0-9\s]', my_str] # 👇️ ['hello', 'one', 'two', 'three', 'four', 'five', 'six'] print[my_list]
1, chữ hoaCopied!
import re my_str = "hellothree.four!five'six" my_list = re.split[r'[^a-zA-Z0-9\s]', my_str] # 👇️ ['hello', 'one', 'two', 'three', 'four', 'five', 'six'] print[my_list]
2, chữ sốCopied!
import re my_str = "hellothree.four!five'six" my_list = re.split[r'[^a-zA-Z0-9\s]', my_str] # 👇️ ['hello', 'one', 'two', 'three', 'four', 'five', 'six'] print[my_list]
3 hoặc khoảng trắngCopied!
import re my_str = "hellothree.four!five'six" my_list = re.split[r'[^a-zA-Z0-9\s]', my_str] # 👇️ ['hello', 'one', 'two', 'three', 'four', 'five', 'six'] print[my_list]
4 ký tự.Copied!
import re my_str = "hellothree.four!five'six" my_list = re.split[r'[^a-zA-Z0-9\s]', my_str] # 👇️ ['hello', 'one', 'two', 'three', 'four', 'five', 'six'] print[my_list]
Bạn có thể thêm bất kỳ ký tự nào mà bạn không muốn khớp giữa các dấu ngoặc vuông của biểu thức chính quy.
Bạn có thể điều chỉnh biểu thức thông thường theo trường hợp sử dụng của bạn. Phần này của các tài liệu có thông tin liên quan đến những gì mỗi nhân vật đặc biệt làm.
Trong khi mã hóa hoặc ứng biến kỹ năng lập trình của bạn, bạn chắc chắn phải bắt gặp nhiều tình huống mà bạn muốn sử dụng .Split [] trong Python không chỉ chia cho một ký tự mà nhiều ký tự cùng một lúc. & NBSP;.split[] in Python not to split on only one character but multiple characters at once.
Example:
"GeeksforGeeks, is an-awesome! website"
Sử dụng .split [] ở trên sẽ dẫn đến.split[] on the above will result in
['GeeksforGeeks, ', 'is', 'an-awesome!', 'website']
trong khi kết quả mong muốn nên
['GeeksforGeeks', 'is', 'an', 'awesome', 'website']
Trong bài viết này, chúng tôi sẽ xem xét một số cách mà chúng tôi có thể đạt được như vậy.
Phương pháp 1: Chia nhiều ký tự từ chuỗi bằng cách sử dụng re.split []
Đây là phương pháp hiệu quả nhất và thường được sử dụng để phân chia nhiều ký tự cùng một lúc. Nó sử dụng regex [biểu thức thông thường] để thực hiện việc này. & Nbsp;
Python3
5Copied!
import re my_str = "hellothree.four!five'six" my_list = re.split[r'[^a-zA-Z0-9\s]', my_str] # 👇️ ['hello', 'one', 'two', 'three', 'four', 'five', 'six'] print[my_list]
6Copied!
import re my_str = "hellothree.four!five'six" my_list = re.split[r'[^a-zA-Z0-9\s]', my_str] # 👇️ ['hello', 'one', 'two', 'three', 'four', 'five', 'six'] print[my_list]
7Copied!
import re my_str = "hellothree.four!five'six" my_list = re.split[r'[^a-zA-Z0-9\s]', my_str] # 👇️ ['hello', 'one', 'two', 'three', 'four', 'five', 'six'] print[my_list]
8Copied!
import re my_str = "hellothree.four!five'six" my_list = re.split[r'[^a-zA-Z0-9\s]', my_str] # 👇️ ['hello', 'one', 'two', 'three', 'four', 'five', 'six'] print[my_list]
9Copied!
import re my_str = "hellothree.four!five'six" my_list = re.split[r'[^a-zA-Z0-9\s]', my_str] # 👇️ ['hello', 'one', 'two', 'three', 'four', 'five', 'six'] print[my_list]
"GeeksforGeeks, is an-awesome! website"0
"GeeksforGeeks, is an-awesome! website"1
"GeeksforGeeks, is an-awesome! website"2
"GeeksforGeeks, is an-awesome! website"3
"GeeksforGeeks, is an-awesome! website"4
"GeeksforGeeks, is an-awesome! website"5
8Copied!
import re my_str = "hellothree.four!five'six" my_list = re.split[r'[^a-zA-Z0-9\s]', my_str] # 👇️ ['hello', 'one', 'two', 'three', 'four', 'five', 'six'] print[my_list]
re.split[]
5re.split[]
6re.split[]
7re.split[]
8re.split[]
9__90908788888"GeeksforGeeks, is an-awesome! website"0
"GeeksforGeeks, is an-awesome! website"1
['GeeksforGeeks, ', 'is', 'an-awesome!', 'website']2
"GeeksforGeeks, is an-awesome! website"3
['GeeksforGeeks, ', 'is', 'an-awesome!', 'website']4
['GeeksforGeeks, ', 'is', 'an-awesome!', 'website']5
Output:
Chuỗi ban đầu là: Let LetS_Try, đây là danh sách sau khi thực hiện chức năng chia tách: [Let Let ,,
Các lớp nhân vật, or _ or – or !. The symbol “|” represents or. There are some symbols in regex which are treated as special symbols and have different functions. If you wish to split on such a symbol, you need to escape it using a “\“[back-slash]. List of special characters that need to be escaped before using them:
. \ + * ? [ ^ ] $ [ ] { } = ! | : -
Example:
Python3
5Copied!
import re my_str = "hellothree.four!five'six" my_list = re.split[r'[^a-zA-Z0-9\s]', my_str] # 👇️ ['hello', 'one', 'two', 'three', 'four', 'five', 'six'] print[my_list]
6Copied!
import re my_str = "hellothree.four!five'six" my_list = re.split[r'[^a-zA-Z0-9\s]', my_str] # 👇️ ['hello', 'one', 'two', 'three', 'four', 'five', 'six'] print[my_list]
Regex Cheat-Sheet trên mô tả nhân vật
"GeeksforGeeks, is an-awesome! website"0
['GeeksforGeeks', 'is', 'an', 'awesome', 'website']2
['GeeksforGeeks', 'is', 'an', 'awesome', 'website']3
['GeeksforGeeks', 'is', 'an', 'awesome', 'website']4
Output:
['GeeksforGeeks', ' is', 'an', 'awesome', ' app', 'too']
Lớp nhân vật tốc ký To know more about regex click here.
Đại diện
\ d.split[] method, it uses a method called .findall[]. This method finds all the matching instances and returns each of them in a list. This way of splitting is best used when you don’t know the exact characters you want to split upon.
Python3
5Copied!
import re my_str = "hellothree.four!five'six" my_list = re.split[r'[^a-zA-Z0-9\s]', my_str] # 👇️ ['hello', 'one', 'two', 'three', 'four', 'five', 'six'] print[my_list]
6Copied!
import re my_str = "hellothree.four!five'six" my_list = re.split[r'[^a-zA-Z0-9\s]', my_str] # 👇️ ['hello', 'one', 'two', 'three', 'four', 'five', 'six'] print[my_list]
Bất kỳ chữ số số nào từ 0 đến 9
"GeeksforGeeks, is an-awesome! website"0
"GeeksforGeeks, is an-awesome! website"1
"GeeksforGeeks, is an-awesome! website"2
"GeeksforGeeks, is an-awesome! website"3
"GeeksforGeeks, is an-awesome! website"4
"GeeksforGeeks, is an-awesome! website"5
8Copied!
import re my_str = "hellothree.four!five'six" my_list = re.split[r'[^a-zA-Z0-9\s]', my_str] # 👇️ ['hello', 'one', 'two', 'three', 'four', 'five', 'six'] print[my_list]
re.split[]
5re.split[]
6re.split[]
7re.split[]
8re.split[]
9__90908788888"GeeksforGeeks, is an-awesome! website"0
"GeeksforGeeks, is an-awesome! website"1
['GeeksforGeeks, ', 'is', 'an-awesome!', 'website']2
"GeeksforGeeks, is an-awesome! website"3
['GeeksforGeeks, ', 'is', 'an-awesome!', 'website']4
['GeeksforGeeks, ', 'is', 'an-awesome!', 'website']5
Output:
Chuỗi ban đầu là: Đây, là - một: Ví dụ ?! Danh sách sau khi thực hiện chức năng phân chia: [‘Cái này
Ở đây, từ khóa [\ w,]+ chỉ ra rằng nó sẽ tìm thấy tất cả các trường hợp của bảng chữ cái hoặc dấu gạch dưới [_] một hoặc nhiều và trả lại chúng trong một danh sách. Lưu ý: [\ w,]+ won chia tách khi gạch dưới [_] khi nó tìm kiếm bảng chữ cái cũng như gạch dưới. & Nbsp;Note: [\w’]+ won’t split upon an underscore[_] as it searches for alphabets as well as underscores.
Example:
Python3
5Copied!
import re my_str = "hellothree.four!five'six" my_list = re.split[r'[^a-zA-Z0-9\s]', my_str] # 👇️ ['hello', 'one', 'two', 'three', 'four', 'five', 'six'] print[my_list]
6Copied!
import re my_str = "hellothree.four!five'six" my_list = re.split[r'[^a-zA-Z0-9\s]', my_str] # 👇️ ['hello', 'one', 'two', 'three', 'four', 'five', 'six'] print[my_list]
['GeeksforGeeks', ' is', 'an', 'awesome', ' app', 'too']8
8Copied!
import re my_str = "hellothree.four!five'six" my_list = re.split[r'[^a-zA-Z0-9\s]', my_str] # 👇️ ['hello', 'one', 'two', 'three', 'four', 'five', 'six'] print[my_list]
['This', 'is', 'underscored', '_', 'example']0
"GeeksforGeeks, is an-awesome! website"0
['This', 'is', 'underscored', '_', 'example']2
. \ + * ? [ ^ ] $ [ ] { } = ! | : -8
['This', 'is', 'underscored', '_', 'example']4
Output:
['This', 'is', 'underscored', '_', 'example']
Chia nhiều ký tự từ chuỗi bằng cách sử dụng thay thế [] và split []
Đây là một cách rất tân binh để chia tách. Nó không sử dụng regex và không hiệu quả nhưng vẫn đáng để thử. Nếu bạn biết các ký tự bạn muốn chia tách, chỉ cần thay thế chúng bằng một không gian và sau đó sử dụng .Split []: & nbsp;.split[]:
Python3
7Copied!
import re my_str = "hellothree.four!five'six" my_list = re.split[r'[^a-zA-Z0-9\s]', my_str] # 👇️ ['hello', 'one', 'two', 'three', 'four', 'five', 'six'] print[my_list]
8Copied!
import re my_str = "hellothree.four!five'six" my_list = re.split[r'[^a-zA-Z0-9\s]', my_str] # 👇️ ['hello', 'one', 'two', 'three', 'four', 'five', 'six'] print[my_list]
['This', 'is', 'underscored', '_', 'example']7
"GeeksforGeeks, is an-awesome! website"0
"GeeksforGeeks, is an-awesome! website"1
"GeeksforGeeks, is an-awesome! website"2
"GeeksforGeeks, is an-awesome! website"3
"GeeksforGeeks, is an-awesome! website"4
"GeeksforGeeks, is an-awesome! website"5
8Copied!
import re my_str = "hellothree.four!five'six" my_list = re.split[r'[^a-zA-Z0-9\s]', my_str] # 👇️ ['hello', 'one', 'two', 'three', 'four', 'five', 'six'] print[my_list]
re.split[]
5re.split[]
6re.split[]
7re.split[]
8re.split[]
9__90908788888"GeeksforGeeks, is an-awesome! website"0
"GeeksforGeeks, is an-awesome! website"1
['GeeksforGeeks, ', 'is', 'an-awesome!', 'website']2
"GeeksforGeeks, is an-awesome! website"3
['GeeksforGeeks, ', 'is', 'an-awesome!', 'website']4
['GeeksforGeeks, ', 'is', 'an-awesome!', 'website']5
Output:
Chuỗi ban đầu là: Let LetS_Try, đây là danh sách sau khi thực hiện chức năng chia tách: [Let Let ,,
Các lớp nhân vật
Regex Cheat-Sheet trên mô tả nhân vật
\ d | Bất kỳ chữ số số nào từ 0 đến 9 |
\ D | Bất kỳ ký tự nào không phải là một chữ số từ 0 đến 9 |
\ w | Bất kỳ chữ cái, chữ số số hoặc ký tự dấu gạch dưới |
\ W | Bất kỳ ký tự nào không phải là chữ cái, chữ số số hoặc ký tự dấu gạch dưới |
\S | Bất kỳ không gian, tab hoặc ký tự mới |
\S | Bất kỳ ký tự nào không phải là không gian, tab hoặc newline |