String search with "|" character

2

I need to perform a query in a txt file in Python, however I have 2 problems due to the interpretation of special characters.

When I enter | or \ the result ends up being replaced: | is interrupted as white space and \ is duplicated ( \ ).

import re

erro = r"FIM|2|"
linha = 'ahsuasaudsdad '

if re.search(erro, linha):
   print('Não deveria cair aqui')
   print(re.findall(erro, linha))

In this case, I search the string FIM|2| within a file, but anyway it returns true in if and my intention is to return true only if string contains FIM|2| .

    
asked by anonymous 04.09.2018 / 15:45

1 answer

5

The problem is that the | character is reserved in the regular expression. What is happening is that you are searching for the word "END" or the number "2" or empty. The emptiness will always be found.

To solve the problem, you need to escape the character:

import re

erro = r"FIM\|2\|"
linha = 'ahsuasaudsdad '

if re.search(erro, linha):
   print('Não deveria cair aqui')
   print(re.findall(erro, linha))
else:
    print('Funcionou :D')
    
04.09.2018 / 15:50