-2

I have an initial string of words, like:

abab sbs abc ffuuu qwerty uii onnl ghj

And I would like to be able to extract only the words that do not contain adjacently-repeating characters, so that the above string is returned as:

abc qwerty ghj

How to accomplish this task using Regular Expressions?

1 Answers1

1

I guess the post is open again after a little rewording of the question.
This is moved from the comments, to the answer region.

A while ago I saw this style problem on a question about no duplicate characters
that encompased the entire string. I just translated it to word boundries.

@Michael J Mulligan did a test case for it (see comments).
The links:
'Working Regex test (regex101.com/r/bA2wB0/1) and a working PHP example (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+