[英]Extract words/sentence that occurs before a keyword from a string - Python
[英]Removing words from a sentence before a keyword from a specific list
我有一个关键字列表keywords = [",", "or", "and"]
和一个句子s = built on the kernel, is written largely in
我想从上面列表中的任何关键字之前删除上面句子中的所有单词,在这种情况下是“,”。 所以 Output 将被is written largely in.
import re
keywords = [",", "or", "and"]
s = "built on the kernel, is written largely in"
split_sentence = re.split(',|or|and',s)[1:]
result = "".join(split_sentence)
print(result)
output:
is written largely in
您可以将re.split()
与模式一起使用为|
加入您的keywords
字符串。 它将返回由关键字分隔的字符串列表。 由于您需要list
的最后一个子字符串,您可以使用-1
作为索引来访问它。
例如:
>>> import re
>>> keywords = [",", "or", "and"]
>>> my_str = "Hello, and, or, Stack Overflow"
>>> re.split('|'.join(keywords), my_str)[-1]
' Stack Overflow'
此外,如果您想从结果字符串中删除额外的空格,那么您可以进一步在上述字符串上使用str.strip()
:
>>> re.split('|'.join(keywords), my_str)[-1].strip()
'Stack Overflow'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.