如何忽略python中的空格

时间:2017-12-11 19:36:17

标签: python regex removing-whitespace

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

示例文字:

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 :

1 个答案:

答案 0 :(得分:-2)

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

this is test\s*\(enclosed\s*\("
相关问题