A regular expression to limit words to a specific combination of letters (in any order)

This is a little harder and a bit out of my league. I want to sort the list of words and eliminate those that do not contain a specific set of characters, however these characters can be in any order, and some may occur more than others.

I want the regular expression to look for the words:

e0 or 1 time
a0 or 1 time
t0 or 1 or 2 times

For example, the following will work:

eat tea tate tt a e

The following steps will not work

eats teas tates ttt aa ee

Lookaround Regex is new to me, so I'm not 100% sure of the syntax (any answer using lookaround with an explanation would be awesome). So far, I guess:

Regex regex = new Regex(@"(?=.*e)(?=.*a)(?=.*t)");
lines = lines.Where(x => regex.IsMatch(x)).ToArray(); //'text' is array containing words
+4
source share
2

:

\b(?:e(?!\w*e)|t(?!(?:\w*t){2})|a(?!\w*a))+\b

:

\b             # Start of word
(?:            # Start of group: Either match...
 e             # an "e",
 (?!\w*e)      # unless another e follows within the same word,
|              # or
 t             # a "t",
 (?!           # unless...
  (?:\w*t){2}  # two more t follow within the same word,
 )             # 
|              # or
 a             # an "a"
 (?!\w*a)      # unless another a follows within the same word.
)+             # Repeat as needed (at least one letter)
\b             # until we reach the end of the word.

regex101.com.

( \w , - " ", )

+3

, , , , , .

, , . ( , ?), .

, .


, :

(?<!\S)(?!\w*(?:e\w*){2})(?!\w*(?:a\w*){2})(?!\w*(?:t\w*){3})[eat]+(?!\S)

:

 (?<! \S )
 (?!
      \w* 
      (?: e \w* ){2}
 )
 (?!
      \w* 
      (?: a \w* ){2}
 )
 (?!
      \w* 
      (?: t \w* ){3}
 )
 [eat]+ 
 (?! \S )

, :

\b(?!\w*(?:e\w*){2})(?!\w*(?:a\w*){2})(?!\w*(?:t\w*){3})[eat]+\b

:

 \b                     # Word boundary
 (?!                    # Lookahead, assert Not 2 'e' s
      \w* 
      (?: e \w* ){2}
 )
 (?!                    #  Lookahead, assert Not 2 'a' s
      \w* 
      (?: a \w* ){2}
 )
 (?!                    #  Lookahead, assert Not 3 't' s
      \w* 
      (?: t \w* ){3}
 )
 # At this point all the checks pass, 
 # all thats left is to match the letters.
 # -------------------------------------------------

 [eat]+                 # 1 or more of these, Consume letters 'e' 'a' or 't'
 \b                     # Word boundary
+1

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


All Articles