I have a string to which I'd like to identify more than 10 possible patterns and replace them without using a For
loop
string example =
text = congrats! first recharge of USD 661 is successful & your service is valid till 2019-10-19. dial 0123456789 or click bit.ly/vf_asdqweerw in 46 hours to avail your reward.
expected outcome =
congrats! first recharge of USD <Amount> is successful & your service is valid till <Date>. dial <PhoneNumber> or click <Link> in 46 hours to avail your reward.
I have a dictionary of regex patterns for each value:
dct = {
r"((http(s?)://)|(bit\\.l)|(www.)).+?(?=[, ]|$)": <Link>,
r"(\d{2}[-/.])(\w{1,3}|\d{2})[-/.](\d{2,4})\b" : <Date>,
.....
}
tried How can I do multiple substitutions using regex in python? but with no success
my current solution uses
for k,v in dct.items():
text = re.sub(k,v,text)
I need something more scalable.