+ 4
Regex Sub
Please help with this code. I need to replace what is inside the square brackets with provided input for example r'[]' will change to r'[$#]' if user provides $# as input More description within. https://code.sololearn.com/c9fn1fDCxcZ4/?ref=app
9 odpowiedzi
+ 4
Ok, how about this?
def subs(a,b=[]):
if b:
a = re.sub(f"[{''.join(b)}]",'',a)
return a
+ 4
https://code.sololearn.com/cBEndbHDiJsT/?ref=app
It is what you want to do?
+ 3
import re
a = 're$gex$es a!re$ !int$er$est!ing'
b = re.sub(r'[!$]','',a)
print(b)
print()
def subs(a, b=''): # the 'b='' is needed for your last 'subs'.
p = re.sub(b,'',a)
return p
print(subs('ple#ase$ h&elp #m$e', '[#amp;]'))
print(subs('i! n$eed h$elp', '[!$]'))
print(subs('thank you'))
print ()
+ 3
So is this what you need?
def subs(a,b=[]):
if b:
a = re.sub(f"[{b[0]}]",'',a)
return a
+ 2
arieh No. Plus, you can't change test cases only your code
+ 2
rodwynnejones the second parameter is a list not a string.
['!#'] or ['!','#'] not '[!#]'
+ 2
Russ Yes. Absolutely. But what if the b parameter is like this [ '#', '!' ] , do I join then split again so it can be b[0]?
+ 2
Russ Amazing, thanks.
+ 2
Thanks guys.