使用php regex查找没有重复字符的单词

时间:2014-10-22 19:52:09

标签: php regex string

我有一个初始字符串,例如:

abab sbs abc ffuuu qwerty uii onnl ghj

我希望能够只提取不包含相邻重复字符的单词,以便上面的字符串返回为:

abc qwerty ghj

如何使用正则表达式完成此任务?

1 个答案:

答案 0 :(得分:1)

我想这个帖子在问题稍作修改后再次打开 这将从评论移至答案区域。

不久前,我在一个关于没有重复字符的问题上看到了这种风格问题 包含整个字符串。我刚把它翻译成单词边界。

@Michael J Mulligan为此做了一个测试案例(见评论) 链接:
'工作正则表达式测试(regex101.com/r/bA2wB0/1)和一个有效的PHP示例(ideone.com/7ID8Ct)'

 # For NO duplicate letters anywhere within word characters
 # -----------------------------------------------------------
 # \b(?!\w*(\w)\w*\1)\w+

 \b               # Word boundry
                  # Only word chars now
 (?!              # Lookahead assertion (like a true/false conditional)
                       # It doesn't matter if the assertion is negative or positive.
                       # In this section, the engine is forced to match if it can,
                       # it has no choice, it can't backtrack its way out of here.
      \w* 
      ( \w )           # (1), Pick a word char, any word char
      \w* 
      \1               # Now it is here again
                       # Ok, the expression matched, time to check if the assertion is correct.
 )                # End assertion
 \w+              # Its here now, looks like the assertion let us through
                  # The assert is that no duplicate word chars ahead,
                  # so free to match word chars 'en masse'


 # For ONLY duplicate letters anywhere within word characters
 # just do the inverse. In this case, the inverse is changing 
 # the lookahead assertion to positive (want duplicates).
 # -----------------------------------------------------------
 # \b(?=\w*(\w)\w*\1)\w+