I am trying to change the suffixes of companies such that they are all in a common pattern such as Limited, Limiteed all to LTD.
Here is my code:
JavaScript
x
2
1
re.sub(r"s+?(CORPORATION|CORPORATE|CORPORATIO|CORPORATTION|CORPORATIF|CORPORATI|CORPORA|CORPORATN)", r" CORP", 'ABC CORPORATN')
2
I’m trying 'ABC CORPORATN'
and it’s not converting it to CORP
. I can’t see what the issue is. Any help would be great.
Edit: I have tried the other endings that I included in the regex and they all work except for corporatin (that I mentioned above)
Advertisement
Answer
I see that all te patterns begins with "CORPARA"
, so we can just go:
JavaScript
1
3
1
import re
2
print(re.sub("CORPORAw+", "CORP", 'ABC CORPORATN'))
3
Output:
JavaScript
1
2
1
ABC CORP
2
Same for the possible patterns of limited; if they all begin with "Limit"
, you can
JavaScript
1
3
1
import re
2
print(re.sub("Limitw+", "LTD", 'Shoe Shop Limited.'))
3
Output:
JavaScript
1
2
1
Shoe Shop LTD.
2