Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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_Removing Whitespace - Fatal编程技术网

如何在python中忽略空白

如何在python中忽略空白,python,regex,removing-whitespace,Python,Regex,Removing Whitespace,我正在尝试检查字符串,如果它存在,我不想写入文件,但字符串之间在不同位置有空格,这使得很难捕获 示例文本: this is test (enclosed ("further enclosed... Line to be printed:1 this is test(enclosed ("further enclosed... Line to be interpreted this is test(enclosed("further enclosed... this is test (enclo

我正在尝试检查字符串,如果它存在,我不想写入文件,但字符串之间在不同位置有空格,这使得很难捕获

示例文本:

this is test (enclosed ("further enclosed...
Line to be printed:1
this is test(enclosed ("further enclosed...
Line to be interpreted 
this is test(enclosed("further enclosed...
this is test (enclosed("further enclosed...
预期产量

Line to be printed:1
Line to be printed:2
我尝试了以下代码,但不起作用:

for word in words:
    if 'this is test .*(enclosed .*(\"' not in word :
        fw.write (word)
我正在把它写入一个文件

有人能帮我实现预期的产出吗?如果你需要更多的细节,请告诉我。另外,我不想使用类似的东西

if 'this is test (enclosed (\"' not in word or 'this is test(enclosed(\"' not in word :

我认为问题不在于空格,但你没有逃过括号。 对于空白,可以使用\s代替。 因此,您的正则表达式将是:

this is test\s*\(enclosed\s*\("

不在
中不会查找正则表达式。您必须明确使用正则表达式包。解决此问题的两种方法是:1)在模式中的任何位置放置可选空格(Neos07方法)或2)在使用更简单的模式(使用
re.match
)测试之前,对行进行规范化(使用
re.sub
)。我让您选择最合适的方法。@casimirithippolyte并使用regex包:)是的:使用
re.search
而不是
in
。导入
re
不会将子字符串搜索转换为正则表达式搜索。@Saurabh:
import
语句不会向语言添加功能,而是从导入的模块中提供方法。查看re模块手册页面,查看哪些新方法可用以及如何使用它们。除了OP根本不使用正则表达式,但他在问题中使用了正则表达式标记。@Neos07:我尝试了你的解决方案。不幸的是。它不起作用