且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

查找两个子字符串之间的所有字符串

更新时间:2022-10-20 08:24:46

使用 re.findall() 获取子字符串的每次出现.$ 被认为是正则表达式中的一个特殊字符,意思是 —字符串的结尾"锚点,因此您需要转义 $ 以匹配文字字符.

>>>进口重新>>>s = '@@cat $$ @@dog$^'>>>re.findall(r'@@(.*?)\$', s)[' 猫狗']

要删除前导和尾随空格,您只需将其匹配到捕获组之外即可.

>>>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)