How do I get find a word that contains some specific letter AND one in particular (using regex)?

Question:

Hello everyone and thank you in advance,

I am trying to get a all the words in the following list except for "motiu" and "diomar" using regex and python:

amfora
difamador
difamar
dimorf
dofi
fada
far
farao
farda
fiar
fiord
fira
firar
firma
for
motiu
diomar

The word must not contain a letter outside the list [diomarf], but it must contain an "f"

I don’t know much about regex…I have tried with some, they are getting more complex but I haven’t got the solution yet. Some of the expressions I have tried with are:

> (?:.*f)(?:.*[diomarf])
> (?:.*[diomarf])(?:.*f)
> (?:((?:f)+)(?:[diomarf])*)
> (?:((?:[diomarf])+)(?:f)*)
> (?:((?:[diomarf])*)((?:f)+))
> (?:(((?:f)+)((?:[diomarf])*)))
> (?:((?:f)+((?:[diomarf])*)))

The expression with which I think I got the closest result is:

(?:(((?:f)+)((?:[diomarf])*)))

But it only checks from the first f of the word, for example, for "dimorf" I am only getting the last "f"

Asked By: Eloi Sanchez

||

Answers:

^f[diomarf]*$|^[diomarf]*f[diomarf]*$|^[diomarf]*f$

demo

Explanation:
^f[diomarf]*$ : A string either starts with f and then has any number of characters from this list [diomarf] (including 0!)
OR
^[diomarf]*f[diomarf]*$ the string has f somewhere in the middle
OR
^[diomarf]*f$ f at the end

The previous solution I proposed fails when disallowed characters are added to the end of the string (for example diomarfg).

Old solution for reference:

(?=[diomarf]).*f.*

demo here

Explanation:

(?=[diomarf]) – use positive lookahead to assert that at any point in the string one of the allowed letters is matched.

.*f.* – make sure that the letter f is somewhere in the string.

Answered By: O-O-O

You can write the pattern as:

^[diomar]*f[diomarf]*$

Explanation

  • ^ Start of string
  • [diomar]* Optionally repeat matching one of the allowed chars excluding the f
  • f Match the mandatory f
  • [diomarf]* Optionally repeat matching one of the allowed chars
  • $ End of string

See a regex101 demo.

Answered By: The fourth bird