查找两个子字符串之间的所有字符串 [英] Find all strings that are in between two sub strings
问题描述
我以以下字符串为例:
string = "@@cat $$ @@dog$^"
我想提取所有锁定在@@"和$"之间的字符串,所以输出将是:
["cat","dog"]
我只知道如何提取第一次出现:
导入重新r = re.compile('@@(.*?)$')m = r.search(字符串)如果米:result_str = m.group(1)
想法和想法欢迎就如何捕获它们提出建议.
使用 re.findall()
获取子字符串的每次出现.$
被认为是正则表达式中的一个特殊字符,意思是 —字符串的结尾"锚点,因此您需要转义 $
以匹配文字字符.
要删除前导和尾随空格,您只需将其匹配到捕获组之外即可.
<预><代码>>>>re.findall(r'@@\s*(.*?)\s*\$', s)['猫狗']此外,如果上下文有可能跨越换行符,您可以考虑使用否定.
<预><代码>>>>re.findall(r'@@\s*([^$]*)\s*\$', s)I have the following string as an example:
string = "@@ cat $$ @@dog$^"
I want to extract all the stringa that are locked between "@@" and "$", so the output will be:
[" cat ","dog"]
I only know how to extract the first occurrence:
import re
r = re.compile('@@(.*?)$')
m = r.search(string)
if m:
result_str = m.group(1)
Thoughts & suggestions on how to catch them all are welcomed.
Use re.findall()
to get every occurrence of your substring. $
is considered a special character in regular expressions meaning — "the end of the string" anchor, so you need to escape $
to match a literal character.
>>> import re
>>> s = '@@ cat $$ @@dog$^'
>>> re.findall(r'@@(.*?)\$', s)
[' cat ', 'dog']
To remove the leading and trailing whitespace, you can simply match it outside of the capture group.
>>> re.findall(r'@@\s*(.*?)\s*\$', s)
['cat', 'dog']
Also, if the context has a possibility of spanning across newlines, you may consider using negation.
>>> re.findall(r'@@\s*([^$]*)\s*\$', s)
这篇关于查找两个子字符串之间的所有字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!