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"
CodePudding user response:
^f[diomarf]*$|^[diomarf]*f[diomarf]*$|^[diomarf]*f$
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.*
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.