PHP 正则表达式替换以 @ 结尾以 2 个空格结尾的单词

PHP regex to replace words starting with @ end ending with 2 spaces

在 PHP 中,如何去掉以 @ (at) 符号开头的单词后面的所有双空格?

示例输入:

This is  an @example  input.

示例输出:

This is  an @example output.

匹配一个以@开头的单词,重置匹配输出,之后匹配多个空格然后进行替换:

preg_replace('~@\w+\K\s{2,}~', ' ', $input_string);

正则表达式解释:

@\w+    # Match a `@` fowlling a word
\K      # Reset matching process output
\s{2,}  # Match double or more whitespaces

PHP live demo