Replace "&&" with "and" using re.sub in python.
Question: I have a string containing `&&`.  
I want to replace all `&&` which have **SPACE** on both right and left side.

**Sample String** :

    
    
    x&& &&&  && && x 
    

**Desired Output** :

    
    
    x&& &&&  and and x
    

  
**My Code** :

    
    
    import re
    print re.sub(r'\B&&\B','and','x&& &&&  && && x')
    

**My Output**

    
    
    x&& and&  and and x
    

Please suggest me, how can I prevent `&&&` from getting replaced with `and&`.


Answer: You can search using this lookaround regex:

    
    
    (?<= )&&(?= )
    

and replace by `and`

**Code:**

    
    
    p = re.compile(ur'(?<= )&&(?= )', re.IGNORECASE)
    test_str = u"x&& &&& && && x"
    
    result = re.sub(p, "and", test_str)
    

[RegEx Demo](https://regex101.com/r/gX7wY0/5)

