Regex: match string followed by dot/comma followed by space

57
April 16, 2021, at 00:50 AM

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.

Answer 1

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. 
READ ALSO
Uncaught Error: Call to a member function prepare() on null. Can someone please help me with these? [closed]

Uncaught Error: Call to a member function prepare() on null. Can someone please help me with these? [closed]

Want to improve this question? Update the question so it's on-topic for Stack Overflow

72
“If there is one thing developers like less than writing documentation, it’s responding to unnecessary escalations […] and too many escalations wear down the developers.”

“If there is one thing developers like less than writing documentation, it’s responding to unnecessary escalations […] and too many escalations wear down the developers.”

I am trying to render a recorded DVB transport stream to an Android TV (Sony) using androidmedia classes and MediaSync for synchronization

52
“If there is one thing developers like less than writing documentation, it’s responding to unnecessary escalations […] and too many escalations wear down the developers.”

“If there is one thing developers like less than writing documentation, it’s responding to unnecessary escalations […] and too many escalations wear down the developers.”

I'm planning to create Espresso tests on my app multi-module, and I'm about to create the first Espresso test, but what I'm seeing is that on my app I do not have an AppComponent where I can fake itSince I want to add the test on my feature-module, I'll create...

56