Một giải pháp regex cho vui:
>>> import re
>>> re.findall[r'@[\w+]', '@Hello there @bob @!']
['Hello', 'bob']
>>> re.findall[r'@[\w+]', 'Hello there bob !']
[]
>>> [re.findall[r'@[\w+]', 'Hello there @bob !'] or None,][0]
'bob'
>>> print [re.findall[r'@[\w+]', 'Hello there bob !'] or None,][0]
None
Regex ở trên sẽ nhận các mẫu của một hoặc nhiều ký tự chữ và số theo ký tự '@' cho đến khi tìm thấy một ký tự không phải là vô sinh.
Dưới đây là giải pháp Regex để khớp với một hoặc nhiều ký tự không phải là màu nếu bạn muốn chụp một phạm vi phụ hơn:
>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
Lưu ý rằng khi Regex ở trên gặp một chuỗi như @xyz@abc
, nó sẽ nắm bắt xyz@abc
trong một kết quả thay vì xyz
và abc
riêng biệt. Để khắc phục điều đó, bạn có thể sử dụng lớp ký tự \s
phủ định đồng thời phủ định các ký tự @
:
>>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']
Và đây là một giải pháp Regex để khớp với một hoặc nhiều ký tự bảng chữ cái chỉ trong trường hợp bạn không muốn có bất kỳ số nào hoặc bất cứ thứ gì khác:
>>> re.findall[r'@[[A-Za-z]+]', '@Hello there @bobv2.0 @!']
['Hello', 'bobv']
Đôi khi chúng ta đi qua các tình huống mà chúng ta yêu cầu để có được tất cả các từ có trong chuỗi, đây có thể là một nhiệm vụ tẻ nhạt được thực hiện bằng phương pháp gốc. Do đó có tốc ký để thực hiện nhiệm vụ này luôn hữu ích. Ngoài ra, bài viết này cũng bao gồm các trường hợp trong đó các dấu chấm câu phải bị bỏ qua. Phương pháp nếu người ta muốn hoàn thành nhiệm vụ cụ thể này. Nhưng nhược điểm là nó thất bại trong các trường hợp chuỗi chứa dấu chấm câu. & NBSP;
Method #1 : Using split[]
Using the split function, we can split the string into a list of words and this is the most generic and recommended
method if one wished to accomplish this particular task. But the drawback is that it fails in cases the string contains punctuation marks.
Python3
>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
0____11 >>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
2>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
3 >>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
4>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
5 ________ 16 & nbsp; >>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
7>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
8>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
1 xyz
2xyz
3>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
6xyz
5>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
6xyz
7xyz
8>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
3 >>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
4>>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']
3 ________ 16 & nbsp; >>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']
5>>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']
6Đầu ra: & nbsp; chuỗi gốc là: geeksforgeek, là tốt nhất @# cổng khoa học máy tính. !!! & nbsp; danh sách các từ là: ['geeksforgeek', 'là', 'tốt nhất', 'máy tính', 'khoa học' , 'Cổng thông tin'] & nbsp; & nbsp;
The original string is : Geeksforgeeks is best Computer Science
Portal
The list of words is : [‘Geeksforgeeks’, ‘is’, ‘best’, ‘Computer’, ‘Science’, ‘Portal’]
Làm cách nào để trích xuất một chuỗi sau một ký tự trong Python?
Method #2 : Using regex[ findall[] ]
In the cases which contain all the special characters and punctuation marks, as discussed above, the conventional method of finding words in string using split can fail and hence requires regular expressions to perform this task. findall function returns the list after filtering the string and
extracting words ignoring punctuation marks.
Python3
>>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']
7 >>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']
8>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
0____11 >>> re.findall[r'@[[A-Za-z]+]', '@Hello there @bobv2.0 @!']
['Hello', 'bobv']
1>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
3 >>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
4>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
5 ________ 16 & nbsp; >>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
7>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
8>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
1 xyz
2xyz
3>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
6xyz
5>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
6xyz
7xyz
8>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
3 >>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
4>>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']
3 ________ 16 & nbsp; >>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']
5>>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']
6Đầu ra: & nbsp; chuỗi gốc là: geeksforgeek, là tốt nhất @# cổng khoa học máy tính. !!! & nbsp; danh sách các từ là: ['geeksforgeek', 'là', 'tốt nhất', 'máy tính', 'khoa học' , 'Cổng thông tin'] & nbsp; & nbsp;
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The list of words is :
[‘Geeksforgeeks’, ‘is’, ‘best’, ‘Computer’, ‘Science’, ‘Portal’]
& nbsp; & nbsp; Phương thức #3: Sử dụng regex [] + string.punning & nbsp; Phương pháp này cũng sử dụng các biểu thức chính quy, nhưng chức năng chuỗi của tất cả các dấu chấm câu được sử dụng để bỏ qua tất cả các dấu chấm câu và nhận chuỗi kết quả được lọc. & NBSP;
Method #3 : Using regex[] + string.punctuation
This method also used regular expressions, but string function of getting all the punctuations is used to ignore all the punctuation marks and get the filtered result string.
Python3
>>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']
7 >>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']
8>>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']
7 xyz@abc
1>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
0____11 >>> re.findall[r'@[[A-Za-z]+]', '@Hello there @bobv2.0 @!']
['Hello', 'bobv']
1>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
3 >>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
4>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
5 ________ 16 & nbsp; >>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
7>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
8>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
1 xyz
2xyz
3>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
6xyz
5>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
6xyz
7xyz
8>>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
3 >>> re.findall[r'@[\S+?]', '@Hello there @bob @!']
['Hello', 'bob', '!']
4>>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']
3 ________ 16 & nbsp; >>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']
5>>> re.findall[r'@[[^\s@]+]', '@xyz@abc some other stuff']
['xyz', 'abc']
6Đầu ra: & nbsp; chuỗi gốc là: geeksforgeek, là tốt nhất @# cổng khoa học máy tính. !!! & nbsp; danh sách các từ là: ['geeksforgeek', 'là', 'tốt nhất', 'máy tính', 'khoa học' , 'Cổng thông tin'] & nbsp; & nbsp;
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The list of words is : [‘Geeksforgeeks’, ‘is’, ‘best’, ‘Computer’, ‘Science’, ‘Portal’]