Home > Mobile >  Generate Regex for match all words whose final letter is not to be found elsewhere in the word
Generate Regex for match all words whose final letter is not to be found elsewhere in the word

Time:02-26

Trying to find the regex to find the last character of the string

CodePudding user response:

\b(?!\w*(\w)(?=\w*\1\b))\w \b

Test on regex101 here

This regex uses a positive lookahead inside a negative lookahead.
The positive lookahead detects characters that match the last character.
The negative lookahead rejects if such 1 was found.

CodePudding user response:

You can match the following regular expression.

(?i)(?<!\w)(?!\w*(\w)\w*\1\b)\w 

Demo

The expression can be broken down as follows.

(?i)      # assert matches are case-independent
(?<!\w)   # negative lookbehind asserts that the previous character is not
          # a word character
(?!       # begin negative lookahead
  \w*     # match zero or more word characters
  (\w)    # match a word character and save to it to capture group 1 
  \w*     # match zero or more word characters
  \1\b    # match the content of capture group 1 followed by a word boundary
)         # end negative lookahead
\w        # match one or more characters
  • Related