How do I remove the leading and trailing non-alpha characters in a given string, before and after a certain substring? See the example below
input_string = m#12$my#tr!#$g%
output_string = m12my#tr!g
The substring, in this case, is my#tr!
How can get the output_string given the input_string?
My attempt below removes all the leading characters (including alphanumeric). See the code snippet below). I tried amending \W+
instead of .+
which did not work.
import re
input_string = "m#12$my#tr#$%"
output_string = re.sub(r'.+?(?=my#tr!)', '', "m#12$my#tr!#$g%")
Appreciate any thought on how I could use the regex pattern for this purpose.
One way to do this is to split the string around the desired substring, replace the non-alphanumeric characters in the first and last parts and then reassemble the string:
Output:
If you use the
regex
module from PyPi, you can take advantage of variable length lookbehinds and replace any non-alphanumeric character that is before or after the target string:Note that if
mid
contains characters that are special to regex (e.g.. [ { $ ^
etc) you should escape it before use i.e.If you don't want to use regex at all, you could manually strip the non-alphanumeric characters out of the first and last parts. For example: