我在 Python 中遇到正则表达式问题。 问题是“必须检查列表并找出包含字符串的空间”。 代码是:
def testFunction(self, func):
a = re.findall(r'\w+[\ ]*\(.*?\)', func)
j = len(a)
print j
if j<=1:
return True
else:
s = re.findall(r"function *()", func) # cheking
t = len(s) # calculating the lenth
if t<=0:
return True
else:
return False
无论是否存在间隙,它始终返回真值。 示例:它同时检测到“test()”和“test ()” 总之,我正在搜索一个正则表达式,它将搜索字符串中的间隙。它只会检测到“test ()”而不是“test()” 我尝试使用 regex = r'\w+[\](.?)' 正则表达式 = r'^\s*' 但不幸的是不起作用。 这项工作可以通过 test(arg1) & 之间不同的正则表达式来完成 测试(arg1)。 谢谢。
请您参考如下方法:
如果你只关心是否有任何空格,为什么不做类似的事情
return " " in func
