Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13891

Replace All '&' to 'And' with exceptions

$
0
0

I learned regex all from scratch and have been struggling with this regex issue for a while and still haven't solved it.

Input:

exception_lst = ['r&b','m&a']str_lst = ['fe r&b fe','sar&bd','sd r &B b','as r& b ac','sdf O&B sd','df R&B sdc','s r&p es','sf m & A df']

Expected Output:

str_lst = ['fe r&b fe','sar and bd','sd r&b b','as r&b ac','sdf O and B sd','df R&B sdc','s r and p es','sf m&a df']

What I want to achieve is

  1. Change all & to ' and ' except the combinations in the exception list.
  2. The exception list can be dynamically inputted.
  3. The exception list is not case-sensitive.
  4. Regex can still capture the exception if there are spaces in between. e.g. r & b and r& b are the same exception as r&b.
  5. Regex can only identify the exception if it is surrounded by spaces. e.g. sar&bd is not an exception.

I tried several here, but none of them met all the requirements.

Demo1

Demo2


Viewing all articles
Browse latest Browse all 13891

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>