Himmators
Himmators

Reputation: 15006

make sure that punctuation is always placed in the right way

The following characters: .?!,… (maybe more, open for suggestions), should always be outputted directly after a word, and always with a space after it.

correct:

This is a fox. It is Brown.

Wrong:

This is a fox . It is Brown.
This is a fox.It is Brown
This is a fox .It is Brown.

I basically want any punctuation. except this:

[^\s][\.?!,…][\s]

Upvotes: 0

Views: 30

Answers (2)

The fourth bird
The fourth bird

Reputation: 163362

You might use lookarounds if supported to assert what is on the left is not a whitespace char and what is on the right is not a non whitespace char

(?<!\s)[.?!,…](?!\S)

Regex demo

Edit

If you want to match the opposite and replace with a dot and space, you might use an alternation with 2 capturing groups and a callback function.

\s+([.?!,…])\s*|([^\s.?!,…][.?!,…])(?!\s)

Regex demo

const regex = /\s+([.?!,…])\s*|([^\s.?!,…][.?!,…])(?!\s)/g;
let str = `This is a fox. It is Brown.

This is a fox . It is Brown.
This is a fox.It is Brown
This is a fox .It is Brown.
`;

str = str.replace(regex, function(_, g1, g2) {
  return (g1 || g2) + " ";
});
console.log(str);

Upvotes: 2

SRG
SRG

Reputation: 345

this is one way

print(re.search(r'.*(\.)(\s).*',str1).group())

Upvotes: 0

Related Questions