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
- Change all & to ' and ' except the combinations in the exception list.
- The exception list can be dynamically inputted.
- The exception list is not case-sensitive.
- 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.
- 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.