i want to turn my text = "hello this is me, and only me. be carefull from 911. "
into: text = "hello this is me,<break></break> and only me.<break></break> be carefull from 911. "
only for strings followed by dot or comma not number.
i tried with this expression: r"\w+([.,])+\s*"
but it match also numbers.
You can use
re.sub(r'([^\W\d_][.,])(\s+)', r'\1<break></break>\2', text)
See the regex demo .
Details :
([^\W\d_][.,])
- Group 1 ( \1
): any letter and then a .
or ,
(\s+)
- Group 2 ( \2
): one or more whitespace chars. See the Python demo :
import re
text = "hello this is me,<break></break> and only me.<break></break> be carefull from 911. "
print(re.sub(r'([^\W\d_][.,])(\s+)', r'\1<break></break>\2', text))
# => hello this is me,<break></break> and only me.<break></break> be carefull from 911.
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.