简体   繁体   中英

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

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"

^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.

You can write the pattern as:

^[diomarf]*?f[diomarf]*$

Explanation

  • ^ Start of string
  • [diomarf]*? Optionally repeat matching one of the allowed chars, as few as possible
  • f Match the mandatory f
  • [diomarf]* Optionally repeat matching one of the allowed chars
  • $ End of string

See a regex101 demo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM