简体   繁体   中英

How to replace multiple instances that match one pattern, with a modified version of that instance using RegEx in Python?

In a sentence containing multiple words, separated by spaces, I have to replace does n't with doesn't . So I'm specifically looking for a space that occurs before a character followed by a '

Regex should be something like this [a-zA-z]* [a-zA-z]'[a-zA-z]
The resulting string should be like this [a-zA-z]*'[a-zA-z]

There could be multiple instances of the matching pattern. In the above example the matching pattern is s n't . Basically I'm looking to remove the space.

You can make use of a positive lookbehind and lookahead to make sure you capture spaces which rest in between your conditions:

(?<=[a-zA-z]) (?=[a-zA-Z]'[a-zA-Z])

https://regex101.com/r/zUCbo0/1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM