effe
effe

Reputation: 1365

regex to match entire words containing (one or more) mandatory and allowed characters

I want to match entire words that containing at least one of mandatory chars and allowed chars.

For example
Mandatory chars are : [ t , a , x ]
Allowed chars are : [ i , e]

t     : passed (one of mandatories are here)
tea   : passed (two of mandatories(t,a) and one allowed(e) here)
e     : failed (none of mandatory is here)
teas  : failed (two of mandatories(t,a) and one allowed(e) here but one intruder(s))

What is the appropriate REGEX code for this? It will be used for search 12.000 rows of MySQL table containing one word each row as a PHP project.

Upvotes: 1

Views: 638

Answers (3)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89639

You can use this pattern:

\b[ie]*+[taxie]++\b

explanation:

since [ie]*+ has a word boundary on the left and a possessive quantifier, it grab all i and e as possible and will never give them back, then the next character must be a t, an a or an x from the next class with the + quantifier that impose at least 1 character.

The word boundary on the right disallow other kind of characters.

Upvotes: 0

Zach Leighton
Zach Leighton

Reputation: 1941

In perl it would be..

$string =~ /^[tax]*[ie]+$/i; #i is for ignore case

* is a 1 or more + is a 0 or more

I just realized you wanted entire words hold on let me rewrite it..

the ^ and $ will match start and end of line.

Upvotes: 0

Thomas
Thomas

Reputation: 182083

Rather than giving a straight answer, let me help you help yourself. A word that passes consists of a sequence of:

  • zero or more allowed or mandatory characters
  • a mandatory character
  • zero or more allowed or mandatory characters

Write regexes for each of these, then just concatenate them to get a regex for the entire thing.

Upvotes: 6

Related Questions