문제

I have lots of input sentences I want to normalize. What they have in common is that they don't have spaces after commas and periods.

Oval,delicate cupcakes.Very tasty.Enjoy.

What is a quickest way to normalize such sentences?

도움이 되었습니까?

해결책

You can use:

$sentence = preg_replace('/([,.])(?!\s)/', '$1 ', $sentence);
  • [.,] - match either a dot or comma
  • ([.,]) - match and group for backreference
  • (?!\s) is negative lookahead which means match if not followed by a space

다른 팁

Try this regex:

$string = preg_replace("/([,.])(?!\s|$)/", "$1 ", $string);

Using negative lookahead (?!\s|$) it is checking whether the , or . is not before a space or end of line.

If you want to get more detail explanation of the regex, just use this link.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top