M. Rogers
M. Rogers

Reputation: 47

Python Use Regex Sub Multiple Times in 1 Pass

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

Answers (1)

Eric
Eric

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

Related Questions