Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将字符串置于两个等号之间_Python_Regex - Fatal编程技术网

Python 将字符串置于两个等号之间

Python 将字符串置于两个等号之间,python,regex,Python,Regex,我试图使用Python的RE来获取两个等号(=helloThere=)之间的信息,但不获取多个等号(==helloThere=)之间的信息 到目前为止,我已经想到了这一点,但它没有做到: result = re.findall('={1}(.*?)={1}', text) 使用负向后看和负向前看 >>> import re >>> re.findall(r'(?<!=)=([^=]+)=(?!=)', '=hello there=') ['hello

我试图使用Python的RE来获取两个等号(
=helloThere=
)之间的信息,但不获取多个等号(
==helloThere=
)之间的信息

到目前为止,我已经想到了这一点,但它没有做到:

result = re.findall('={1}(.*?)={1}', text)

使用负向后看和负向前看

>>> import re
>>> re.findall(r'(?<!=)=([^=]+)=(?!=)', '=hello there=')
['hello there']
>>> re.findall(r'(?<!=)=([^=]+)=(?!=)', '==hello there==')
[]
>>重新导入

>>>关于findall(r’(?使用负向后看和负向前看

>>> import re
>>> re.findall(r'(?<!=)=([^=]+)=(?!=)', '=hello there=')
['hello there']
>>> re.findall(r'(?<!=)=([^=]+)=(?!=)', '==hello there==')
[]
>>重新导入

>>>关于findall(r’(?如果您只有这两个条件:

1-以
=

2-应该只有两个
=

那么我给你的建议是:

s = '=helloThere='
test_char = '='
if (s[0] == s[-1] == test_char) and s.count('=') == 2:
    print(s[1:-1])
    # or print(s.strip('=')

如果
=
之间的表达式不包含另一个
=

表达式(如果只有这两个条件),则此选项有效:

1-以
=

2-应该只有两个
=

那么我给你的建议是:

s = '=helloThere='
test_char = '='
if (s[0] == s[-1] == test_char) and s.count('=') == 2:
    print(s[1:-1])
    # or print(s.strip('=')

如果
=
之间的表达式不包含另一个
=

则此选项有效。谢谢,这正是我要查找的内容。谢谢,这正是我要查找的内容。感谢您的回复。我正在查找可以与findall一起使用的内容,因为输入字符串非常长,我希望找到该内容的多次出现是的,正则表达式是你的解决方案,用我的解决方案,你必须将它包装在for循环中,并计算循环步骤,这将增加正则表达式已经处理过的更多复杂情况。谢谢你的回复。我正在寻找一些我可以使用findall的东西,因为输入字符串很长,而且要查找此模式的多个实例。是的,正则表达式是您的解决方案,使用我的解决方案,您必须将其包装在for循环中,并计算循环步骤,这将增加正则表达式已经处理过的更多复杂性。