Reputation: 47
Is there a way to combine multiple regex statements into one so it can do different subs on a single pass?
no_Punct = re.sub('(\w)([?:!.,;-]+)(\s)',r'\1 ',raw)
no_Punct = re.sub('(\s)([-]+)(\s)',r'\1',no_Punct)
The input string is 'raw'. I am trying to strip certain punctuation at the ends of words and remove hyphens that are surrounded by a space on each side. Can I combine both of these into one statement?
Given the input of: This is a sentence! One-fourth equals .25.
Output is: This is a sentence one fourth equals .25
Upvotes: 0
Views: 500
Reputation: 97591
Trivially, by just substituting one into the other:
no_Punct = re.sub('(\s)([-]+)(\s)', r'\1', re.sub('(\w)([?:!.,;-]+)(\s)', r'\1 ', raw))
Although this may also work:
no_Punct = re.sub('(?<=\w)[?:!.,;-]+(?=\s)|(?<=\s)-+\s', '', raw)
Upvotes: 5