Hướng dẫn how do you split a string by special characters in python? - làm thế nào để bạn chia một chuỗi bằng các ký tự đặc biệt trong python?

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

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)
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ường

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)
1, chữ hoa

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)
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ắng

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)
4 ký tự.

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

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)
5

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)
6

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)
7

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)
8

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)
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

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)
8 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

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)
5

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)
6

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

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)
5

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)
6

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

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)
8 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

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)
5

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)
6

['GeeksforGeeks', ' is', 'an', 'awesome', ' app', 'too']
8

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)
8
['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

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)
7

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)
8
['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

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)
8 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

Lớp nhân vật tốc kýĐại diện
\ dBất kỳ chữ số số nào từ 0 đến 9
\ DBất kỳ ký tự nào không phải là một chữ số từ 0 đến 9
\ wBất kỳ chữ cái, chữ số số hoặc ký tự dấu gạch dưới
\ WBấ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
\SBất kỳ không gian, tab hoặc ký tự mới
\SBất kỳ ký tự nào không phải là không gian, tab hoặc newline

Bạn có thể chia một chuỗi bằng nhiều ký tự trong Python không?

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) để làm điều này.Split multiple characters from string using re. split() This is the most efficient and commonly used method to split multiple characters at once. It makes use of regex(regular expressions) in order to do this.

Làm thế nào để bạn chia một chuỗi bằng một ký tự không bảng chữ cái trong Python?

Để phân chia chuỗi trên các ký tự không phải là ký tự số, bạn có thể sử dụng ký tự đặc biệt \ w, tương đương với [^a-za-z0-9_].use the special character \W , equivalent to [^a-zA-Z0-9_] .

Làm thế nào để bạn tách các số ký tự và ký tự đặc biệt khỏi chuỗi đã cho trong Python?

Xác định độ dài của chuỗi ..
Quét riêng từng ký tự (CH) trong một chuỗi.Thêm nó vào chuỗi res1 nếu (CH là một chữ số).....
In mỗi chuỗi.Chúng ta sẽ có ba chuỗi: một chuỗi có thành phần số, một không có thành phần số và một chuỗi có ký tự đặc biệt ...

Làm thế nào để bạn chia một chuỗi ở một ký tự nhất định?

Để phân chia một chuỗi với ký tự cụ thể làm dấu phân cách trong java, phương thức call split () trên đối tượng chuỗi và chuyển ký tự cụ thể làm đối số cho phương thức Split ().Phương thức trả về một mảng chuỗi với các phân tách dưới dạng các phần tử trong mảng.call split() method on the string object, and pass the specific character as argument to the split() method. The method returns a String Array with the splits as elements in the array.