Search code examples
pythonregexregular-language

How to find logical 'or' operator (||) from a sequence using Regex Python


I want to find and replace the logical "or" operator (||) from my sequence. How can I do it?

Following is the code, I'm trying

import re 
sequence = "if(ab||2) + H) then a*10"
expr = re.sub(r"||","_or_",sequence)
print (expr)

Expected answer should be

if(ab_or_2) + H) then a*10

Solution

  • You need to use escape sequence '\' here since '|' is a special character.

    Python docs:

    '|'

    A|B, where A and B can be arbitrary REs, creates a regular expression that will match either A or B. An arbitrary number of REs can be separated by the '|' in this way. This can be used inside groups (see below) as well. As the target string is scanned, REs separated by '|' are tried from left to right.

    So you need to do :

    expr = re.sub(r"\|\|","_or_",sequence)
    

    Or, using re.escape() : thanks to @Steven for pointing this out

    expr = re.sub(re.escape("||"),"_or_",sequence)
    

    And you will get :

    IN : sequence = "if(ab||2) + H) then a*10"
    OUT : 'if(ab_or_2) + H) then a*10'
    

    Edit :

    If you are not required to just use regex, you can directly use replace for the string. ie,

    sequence.replace('||','_or_')
    

    Here you won't have to worry about the special character.