How can you match words with multiple characters?

I would like to use a regular expression to match all words with more than one character, as opposed to words made entirely from the same char.

This should not match: ttttt, rrrrr, ggggggggggggg

This should match: rttttttt, word, wwwwwwwwwu

+3
source share
5 answers

The following expression will do the trick.

^(?<FIRST>[a-zA-Z])[a-zA-Z]*?(?!\k<FIRST>)[a-zA-Z]+$
  • capture the first character in a group FIRST
  • capture a few more characters (lazy to avoid a return)
  • make sure the following character is different from FIRSTusing a negative statement
  • capture all (at least one due to statement) remaining characters

, , , , , .

.

^(\w)\w*?(?!\1)\w+$

, [a-zA-Z].

+6

,

\ (\ )\1+\

+1

, , , :

public bool Match(string str)
{
    return string.IsNullOrEmpty(str)
               || str.ToCharArray()
                     .Skip(1)
                     .Any( c => !c.Equals(str[0]) );
}
+1

RE , : , . , .

\b(\w)\1*\b
0
\b\w*?(\w)\1*(?:(?!\1)\w)\w*\b

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

, - ; . , , , , :

(.)(?:(?!\1).)

... , . , \w - , [A-Za-z].

0

Source: https://habr.com/ru/post/1707691/


All Articles