Tôi đang cố gắng làm điều này:
import re
sentence = "How are you?"
print[re.split[r'\b', sentence]]
Kết quả là
[u'How are you?']
Tôi muốn một cái gì đó như
[u'How are you?']
9. Làm thế nào điều này có thể đạt được?Khi được hỏi ngày 15 tháng 5 năm 2016 lúc 11:17May 15, 2016 at 11:17
5
Thật không may, Python không thể chia bởi chuỗi trống.
Để khắc phục điều này, bạn sẽ cần sử dụng
import re
sentence = "How are you?"
print[re.findall[r'\w+|\W+', sentence]]
0 thay vì import re
sentence = "How are you?"
print[re.findall[r'\w+|\W+', sentence]]
1.Trên thực tế
import re
sentence = "How are you?"
print[re.findall[r'\w+|\W+', sentence]]
2 chỉ có nghĩa là ranh giới từ.Nó tương đương với
import re
sentence = "How are you?"
print[re.findall[r'\w+|\W+', sentence]]
3.Điều đó có nghĩa là, mã sau sẽ hoạt động:
import re
sentence = "How are you?"
print[re.findall[r'\w+|\W+', sentence]]
Đã trả lời ngày 15 tháng 5 năm 2016 lúc 11:39May 15, 2016 at 11:39
Kenny Laukenny LauKenny Lau
4574 Huy hiệu bạc13 Huy hiệu đồng4 silver badges13 bronze badges
8
import re
split = re.findall[r"[\w']+|[.,!?;]", "How are you?"]
print[split]
Output:
['How', 'are', 'you', '?']
Bản demo IdeOne
Demo Regex101
Giải thích Regex:
"[\w']+|[.,!?;]"
1st Alternative: [\w']+
[\w']+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\w match any word character [a-zA-Z0-9_]
' the literal character '
2nd Alternative: [.,!?;]
[.,!?;] match a single character present in the list below
.,!?; a single character in the list .,!?; literally
Đã trả lời ngày 15 tháng 5 năm 2016 lúc 13:49May 15, 2016 at 13:49
Pedro lobitopedro lobitoPedro Lobito
89.4K30 Huy hiệu vàng239 Huy hiệu bạc259 Huy hiệu Đồng30 gold badges239 silver badges259 bronze badges
2
Đây là cách tiếp cận của tôi đối với
import re
sentence = "How are you?"
print[re.findall[r'\w+|\W+', sentence]]
1 về ranh giới từ:re.split[r"\b\W\b", "How are you?"] # Reprocess list to split on special characters.
# Result: ['How', 'are', 'you?']
và sử dụng
import re
sentence = "How are you?"
print[re.findall[r'\w+|\W+', sentence]]
0 trên ranh giới từre.findall[r"\b\w+\b", "How are you?"]
# Result: ['How', 'are', 'you']
Đã trả lời ngày 7 tháng 9 lúc 17:48Sep 7 at 17:48
Danny Yoo Dyoo tại hkn.eecs.berkeley.edu Thu ngày 14 tháng 8 15:06:12 EDT 2003 Dyoo tại hkn.eecs.berkeley.edu Thu ngày 14 tháng 8 15:06:12 edt 2003
THU ngày 14 tháng 8 15:06:12 EDT 2003
- Tin nhắn trước: [gia sư] chia tách theo ranh giới từ
- Thông báo tiếp theo: [gia sư] chia tách theo ranh giới từ
- Tin nhắn được sắp xếp bởi: [Ngày] [Chủ đề] [Chủ đề] [Tác giả] [ date ] [ thread ] [ subject ] [ author ]
On Thu, 14 Aug 2003, Neil Schemenauer wrote: > Michael Janssen wrote: > > this is important but not enough. re.split[r'\b', 'word boundary'] is > > yet infunctional. I've looked through the sources to find out why. > > re.findall[r'\w+', ...] should do what is intended. Hi Neil, Similarly, we can split against nonwords: ### >>> re.split[r'\W+', 'word boundary'] ['word', 'boundary'] ### But for the reason why '\b' alone isn't splitting: '\b' is a bit special! According to the docs: //www.python.org/doc/lib/re-syntax.html it matches the empty string, but only at the beginning or end of a word. That '\b' matches the empty string is the important key to the mystery, because trying to split against the empty string doesn't do anything: ### >>> re.split[r'', 'word boundary'] ['word boundary'] ### So by the same reasoning, doing '\b' to split won't work, since it's trying to split with the empty string. Hope this helps!
- Tin nhắn trước: [gia sư] chia tách theo ranh giới từ
- Thông báo tiếp theo: [gia sư] chia tách theo ranh giới từ
- Tin nhắn được sắp xếp bởi: [Ngày] [Chủ đề] [Chủ đề] [Tác giả] [ date ] [ thread ] [ subject ] [ author ]
Trong bài viết này, sẽ học cách phân chia một chuỗi dựa trên một mẫu biểu thức thông thường trong Python. Phương pháp Pythons Re Module
import re
sentence = "How are you?"
print[re.findall[r'\w+|\W+', sentence]]
6 chia chuỗi theo các lần xuất hiện của mẫu regex, trả về một danh sách chứa các chuỗi con kết quả.split the string by the occurrences of the regex pattern, returning a list containing the resulting substrings.Sau khi đọc bài viết này, bạn sẽ có thể thực hiện các hoạt động phân chia sau bằng Regex trong Python.
7 | Chia chuỗi theo mỗi lần xuất hiện của 8. |
9 | Chia chuỗi theo các lần xuất hiện của 8. Giới hạn số lượng phân tách ở 2 |
1 | Chia chuỗi phân chia theo nhiều mẫu phân cách [________ 32 & nbsp; và 3]. |
Cách sử dụng hàm import re
sentence = "How are you?"
print[re.findall[r'\w+|\W+', sentence]]
6
import re
sentence = "How are you?"
print[re.findall[r'\w+|\W+', sentence]]
Trước khi di chuyển xa hơn, hãy để Lừa xem cú pháp của Python, ____ ____ 26 & nbsp;
Cú pháp
re.split[pattern, string, maxsplit=0, flags=0]
Mẫu biểu thức chính quy và chuỗi đích là các đối số bắt buộc.
import re
split = re.findall[r"[\w']+|[.,!?;]", "How are you?"]
print[split]
6 và cờ là tùy chọn.
8: mẫu biểu thức chính quy được sử dụng để phân tách chuỗi đích.import re sentence = "How are you?" print[re.findall[r'\w+|\W+', sentence]]
8: Biến chỉ vào chuỗi đích [nghĩa là, chuỗi chúng tôi muốn chia].import re split = re.findall[r"[\w']+|[.,!?;]", "How are you?"] print[split]
6: Số lượng phân tách bạn muốn thực hiện. Nếuimport re split = re.findall[r"[\w']+|[.,!?;]", "How are you?"] print[split]
6 là 2, nhiều nhất là hai lần chia tách và phần còn lại của chuỗi được trả về làm yếu tố cuối cùng của danh sách.import re split = re.findall[r"[\w']+|[.,!?;]", "How are you?"] print[split]
1: Theo mặc định, không có cờ nào được áp dụng. Có nhiều cờ Regex chúng ta có thể sử dụng. Ví dụ,['How', 'are', 'you', '?']
2 được sử dụng để thực hiện tìm kiếm không nhạy cảm trường hợp.['How', 'are', 'you', '?']
There are many regex flags we can use. For example, the
2 is used for performing case-insensitive searching.['How', 'are', 'you', '?']
Lưu ý: Nếu chụp dấu ngoặc đơn được sử dụng trong mẫu, thì văn bản của tất cả các nhóm trong mẫu cũng được trả về như một phần của danh sách kết quả.: If capturing parentheses are used in the pattern, then the text of all groups in the pattern is also returned as part of the resulting list.
Giá trị trả về
Nó chia chuỗi mục tiêu theo mẫu biểu thức chính quy và các trận đấu được trả về dưới dạng danh sách.list.
Nếu mẫu được chỉ định không được tìm thấy bên trong chuỗi đích, thì chuỗi không được phân chia theo bất kỳ cách nào, nhưng phương thức chia vẫn tạo ra một danh sách vì đây là cách mà nó thiết kế. Tuy nhiên, danh sách chỉ chứa một phần tử, chính chuỗi đích.
Ví dụ regex để chia chuỗi thành từ
Bây giờ, hãy để Lừa xem cách sử dụng
import re
sentence = "How are you?"
print[re.findall[r'\w+|\W+', sentence]]
6 với sự trợ giúp của một ví dụ đơn giản. Trong ví dụ này, chúng tôi sẽ phân chia chuỗi mục tiêu ở mỗi ký tự không gian trắng bằng cách sử dụng chuỗi đặc biệt ['How', 'are', 'you', '?']
4. white-space character using the ['How', 'are', 'you', '?']
4 special
sequence.Hãy để thêm
['How', 'are', 'you', '?']
5 metacharacter vào cuối ['How', 'are', 'you', '?']
4. Bây giờ, mẫu regex ['How', 'are', 'you', '?']
7 sẽ phân chia chuỗi mục tiêu về sự xuất hiện của một hoặc nhiều ký tự khoảng trắng. Hãy cùng xem bản demo.Thí dụ
[u'How are you?']
0Như bạn có thể thấy trong đầu ra, chúng tôi có danh sách các từ được phân tách bằng khoảng trắng.
Giới hạn số lượng chia tách
Tham số
import re
split = re.findall[r"[\w']+|[.,!?;]", "How are you?"]
print[split]
6 của import re
sentence = "How are you?"
print[re.findall[r'\w+|\W+', sentence]]
6 được sử dụng để xác định số lượng phân tách bạn muốn thực hiện.Nói một cách đơn giản, nếu
import re
split = re.findall[r"[\w']+|[.,!?;]", "How are you?"]
print[split]
6 là 2, thì hai lần phân tách sẽ được thực hiện và phần còn lại của chuỗi được trả về làm yếu tố cuối cùng của danh sách.if the import re
split = re.findall[r"[\w']+|[.,!?;]", "How are you?"]
print[split]
6 is 2, then two splits will be done, and the remainder of the string is returned as the final element of the list.Vì vậy, hãy để một ví dụ đơn giản để phân chia một chuỗi về sự xuất hiện của bất kỳ chữ số nào. Ở đây chúng tôi sẽ sử dụng trình tự đặc biệt
"[\w']+|[.,!?;]"
1st Alternative: [\w']+
[\w']+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\w match any word character [a-zA-Z0-9_]
' the literal character '
2nd Alternative: [.,!?;]
[.,!?;] match a single character present in the list below
.,!?; a single character in the list .,!?; literally
1 phù hợp với bất kỳ ký tự không chữ số nào.Thí dụ
[u'How are you?']
1Regex để phân chia chuỗi với nhiều dấu phân cách
Trong phần này, chúng tôi sẽ học cách sử dụng Regex để phân chia một chuỗi trên nhiều trình phân cách trong Python.
Ví dụ: sử dụng phương thức biểu thức thông thường
import re
sentence = "How are you?"
print[re.findall[r'\w+|\W+', sentence]]
6, chúng ta có thể chia chuỗi bằng dấu phẩy hoặc không gian.Với phương pháp Regex
"[\w']+|[.,!?;]"
1st Alternative: [\w']+
[\w']+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\w match any word character [a-zA-Z0-9_]
' the literal character '
2nd Alternative: [.,!?;]
[.,!?;] match a single character present in the list below
.,!?; a single character in the list .,!?; literally
3, bạn sẽ linh hoạt hơn. Bạn có thể chỉ định một mẫu cho các trình phân cách nơi bạn có thể chỉ định nhiều trình phân cách, trong khi với phương thức chuỗi ____ "[\w']+|[.,!?;]"
1st Alternative: [\w']+
[\w']+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\w match any word character [a-zA-Z0-9_]
' the literal character '
2nd Alternative: [.,!?;]
[.,!?;] match a single character present in the list below
.,!?; a single character in the list .,!?; literally
3, bạn chỉ có thể sử dụng một ký tự hoặc bộ ký tự cố định để phân chia chuỗi.Hãy để một ví dụ đơn giản để phân chia chuỗi bằng dấu gạch nối hoặc bằng dấu phẩy.
Ví dụ để phân chia chuỗi bởi hai dấu phân cách
[u'How are you?']
2Regex để phân chia chuỗi trên năm dấu phân cách
Ở đây, chúng tôi sẽ sử dụng Regex để phân chia một chuỗi với năm dấu phân cách bao gồm dấu chấm, dấu phẩy, dấu chấm phẩy, dấu gạch nối và không gian theo sau là bất kỳ lượng khoảng trắng nào.
[u'How are you?']
3Lưu ý: Chúng tôi đã sử dụng ký tự
5 meta để biểu thị danh sách các ký tự phân cách. "[\w']+|[.,!?;]"
1st Alternative: [\w']+
[\w']+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\w match any word character [a-zA-Z0-9_]
' the literal character '
2nd Alternative: [.,!?;]
[.,!?;] match a single character present in the list below
.,!?; a single character in the list .,!?; literally
5 phù hợp với bất kỳ ký tự duy nhất trong ngoặc. Ví dụ, "[\w']+|[.,!?;]"
1st Alternative: [\w']+
[\w']+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\w match any word character [a-zA-Z0-9_]
' the literal character '
2nd Alternative: [.,!?;]
[.,!?;] match a single character present in the list below
.,!?; a single character in the list .,!?; literally
7 sẽ khớp với dấu gạch nối, dấu phẩy, dấu chấm phẩy, dấu chấm và ký tự không gian.: we used "[\w']+|[.,!?;]"
1st Alternative: [\w']+
[\w']+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\w match any word character [a-zA-Z0-9_]
' the literal character '
2nd Alternative: [.,!?;]
[.,!?;] match a single character present in the list below
.,!?; a single character in the list .,!?; literally
"[\w']+|[.,!?;]"
1st Alternative: [\w']+
[\w']+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\w match any word character [a-zA-Z0-9_]
' the literal character '
2nd Alternative: [.,!?;]
[.,!?;] match a single character present in the list below
.,!?; a single character in the list .,!?; literally
5 meta character to indicate a list of delimiter characters. The "[\w']+|[.,!?;]"
1st Alternative: [\w']+
[\w']+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\w match any word character [a-zA-Z0-9_]
' the literal character '
2nd Alternative: [.,!?;]
[.,!?;] match a single character present in the list below
.,!?; a single character in the list .,!?; literally
5 matches any single character in brackets. For example, "[\w']+|[.,!?;]"
1st Alternative: [\w']+
[\w']+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\w match any word character [a-zA-Z0-9_]
' the literal character '
2nd Alternative: [.,!?;]
[.,!?;] match a single character present in the list below
.,!?; a single character in the list .,!?; literally
7 will match either hyphen, comma, semicolon, dot, and a space character.Regex để chia chuỗi thành các từ với nhiều trình phân cách ranh giới từ
Trong ví dụ này, chúng tôi sẽ sử dụng mẫu regex ____58 để phục vụ cho bất kỳ phân định không đồngumeric nào. Sử dụng mẫu này, chúng ta có thể phân chia chuỗi bằng nhiều trình phân cách ranh giới từ sẽ dẫn đến một danh sách các mã thông báo chữ và số.
Lưu ý:
"[\w']+|[.,!?;]"
1st Alternative: [\w']+
[\w']+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\w match any word character [a-zA-Z0-9_]
' the literal character '
2nd Alternative: [.,!?;]
[.,!?;] match a single character present in the list below
.,!?; a single character in the list .,!?; literally
9 là một chuỗi đặc biệt của Regex phù hợp với bất kỳ ký tự không phải là tổng số. Không phải là vô cầu có nghĩa là không có chữ cái, chữ số và dấu gạch dưới.Thí dụ
[u'How are you?']
4Chia chuỗi của các dấu phân cách và từ cụ thể
[u'How are you?']
5Regex chia một chuỗi và giữ các dấu phân cách
Như tôi đã nói với bạn khi bắt đầu bài viết nếu chụp dấu ngoặc đơn được sử dụng trong mẫu, thì văn bản của tất cả các nhóm trong mẫu cũng được trả lại như một phần của danh sách kết quả.
Lưu ý: Bạn đang chụp nhóm bằng cách viết mẫu bên trong ____ 60, ________ 61.: You are capturing the group by writing pattern inside the
re.split[r"\b\W\b", "How are you?"] # Reprocess list to split on special characters.
# Result: ['How', 'are', 'you?']
0,re.split[r"\b\W\b", "How are you?"] # Reprocess list to split on special characters.
# Result: ['How', 'are', 'you?']
1.Nói một cách đơn giản, hãy cẩn thận trong khi sử dụng phương pháp
import re
sentence = "How are you?"
print[re.findall[r'\w+|\W+', sentence]]
6 khi mẫu biểu thức chính quy được đặt trong ngoặc đơn để chụp các nhóm. & NBSP; Nếu các nhóm chụp được sử dụng, thì văn bản phù hợp cũng được đưa vào danh sách kết quả.Nó rất hữu ích khi bạn muốn giữ các dấu phân cách/dấu phân cách trong danh sách kết quả.
[u'How are you?']
6Chuỗi phân tách regex bằng cách bỏ qua trường hợp
Có khả năng chuỗi chứa chữ thường và chữ cái trên.
Ví dụ: bạn muốn chia một chuỗi trên các ký tự hoặc phạm vi ký tự cụ thể, nhưng bạn không biết liệu ký tự/từ đó là chữ hoa hay chữ thường hay kết hợp cả hai. Tại đây, bạn có thể sử dụng cờ
re.split[r"\b\W\b", "How are you?"] # Reprocess list to split on special characters.
# Result: ['How', 'are', 'you?']
3 hoặc ['How', 'are', 'you', '?']
2 bên trong phương pháp import re
sentence = "How are you?"
print[re.findall[r'\w+|\W+', sentence]]
6 để thực hiện phân tách không nhạy cảm trường hợp.[u'How are you?']
7Chuỗi phân chia [] Phương thức so với Regex Split []
Bây giờ, hãy để Lừa nghĩ về phương thức
"[\w']+|[.,!?;]"
1st Alternative: [\w']+
[\w']+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\w match any word character [a-zA-Z0-9_]
' the literal character '
2nd Alternative: [.,!?;]
[.,!?;] match a single character present in the list below
.,!?; a single character in the list .,!?; literally
3 mặc định trong Python, đặc trưng cho các chuỗi. Như bạn có thể biết nhiều nhất, phương thức "[\w']+|[.,!?;]"
1st Alternative: [\w']+
[\w']+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\w match any word character [a-zA-Z0-9_]
' the literal character '
2nd Alternative: [.,!?;]
[.,!?;] match a single character present in the list below
.,!?; a single character in the list .,!?; literally
3 mặc định chia một chuỗi theo một dấu phân cách cụ thể. Tuy nhiên, xin lưu ý rằng dấu phân cách này là một chuỗi cố định mà bạn xác định bên trong dấu ngoặc đơn của phương thức.Sự khác biệt giữa các phương thức mặc định
"[\w']+|[.,!?;]"
1st Alternative: [\w']+
[\w']+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\w match any word character [a-zA-Z0-9_]
' the literal character '
2nd Alternative: [.,!?;]
[.,!?;] match a single character present in the list below
.,!?; a single character in the list .,!?; literally
3 và các biểu thức thông thường "[\w']+|[.,!?;]"
1st Alternative: [\w']+
[\w']+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\w match any word character [a-zA-Z0-9_]
' the literal character '
2nd Alternative: [.,!?;]
[.,!?;] match a single character present in the list below
.,!?; a single character in the list .,!?; literally
3 là rất lớn. Có một cách linh hoạt hơn khi sử dụng các biểu thức chính quy được phân chia, có thể chứng minh rất hữu ích trong một số kịch bản và cho các nhiệm vụ cụ thể.- Với phương thức
6, bạn có thể chỉ định một mẫu cho dấu phân cách, trong khi với phương thức mặc địnhimport re sentence = "How are you?" print[re.findall[r'\w+|\W+', sentence]]
3, bạn chỉ có thể sử dụng một ký tự hoặc bộ ký tự cố định."[\w']+|[.,!?;]" 1st Alternative: [\w']+ [\w']+ match a single character present in the list below Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy] \w match any word character [a-zA-Z0-9_] ' the literal character ' 2nd Alternative: [.,!?;] [.,!?;] match a single character present in the list below .,!?; a single character in the list .,!?; literally
- Ngoài ra, sử dụng
6, chúng ta có thể chia một chuỗi bằng nhiều dấu phân cách.import re sentence = "How are you?" print[re.findall[r'\w+|\W+', sentence]]
Chia chuỗi phân chia theo các từ trường hợp trên
Ví dụ: bạn có một chuỗi như là Emma yêu thích Python và ML, và bạn muốn chia nó bằng các từ chữ hoa để có kết quả như [‘Xin chào,’ ’thế nào,’ bạn, bạn]
[u'How are you?']
8Giải trình
- Chúng tôi đã sử dụng Lookahead Regex
3.re.findall[r"\b\w+\b", "How are you?"] # Result: ['How', 'are', 'you']
- Regex này sẽ phân chia ở mọi không gian [____ 44], theo sau là một chuỗi các chữ cái trên [[____ 75]] kết thúc bằng một giới hạn từ [________ 22].