![](/img/trans.png)
[英]How to get rid of words with special characters in Python using regular expressions?
[英]How to use regular expressions in python to capture the characters between two words?
如何在python中使用正則表達式來捕獲兩個單詞之間的字符串? 另外,只返回兩個單詞之間出現的第一組。
例如,我想捕獲“Hi”和“Best”之間的所有單詞
original_string =“文本文字你好,我需要幫助使用正則表達式,最好克里斯更多的單詞嗨這不是我想要的,最好的”
desired_string =“那里,我需要使用正則表達式的幫助,”
我知道你使用這個:(?s)(?<= start_delim)。+?(?= end_delim)用於c#並根據需要替換start_delim和end_delim。
到目前為止,我正試圖用搜索搜索這兩個單詞:
b = re.search("Hi(.*?)/Best", a)
您需要調用group
方法來返回匹配的字符串:
>>> import re
>>> original_string = "text text words Hi there, I need help using regular expressions ,Best Chris more words Hi This is not what I want ,Best"
>>> desired_string = re.search("(?s)(?<=Hi).+?(?=Best)", original_string).group()
>>> desired_string
' there, I need help using regular expressions ,'
>>>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.