Does regex match any word containing all the specific letters?

I am trying to collect a regular expression that matches any word containing all the specified letters.

For instance:

a,b

Aaron: not match
Abby: match
Barry: match
Bobb: not match
Alley: not match
Abel: match
Bella: match

any ideas?

+4
source share
4 answers
(?=\w*a)(?=\w*b)\w+

Before moving to a space or character other than a word, mark “a” and “b”. Then write down all the characters to the end of the word.

+4
source

Here is a simple approach.

(.*[Aa].*[bB])|(.*[Bb].*[Aa])

This will require at least a and b.

You can find more information and tutorials on these and other topics here .

+3
source
(\w*[Aa]\w*[bB]\w*)|(\w*[Bb]\w*[Aa]\w*)

David's approach led me to this. But one of the problems with David's original sentences is that this requires a match with A or B.

+1
source

This is the one I would use (in Python syntax):

word_with_a_and_b = re.compile(r"""
    # Match word having both "a" and "b".
    \b            # Anchor to start of word.
    (?=[^\Wa]*a)  # Assert word contains an "a".
    (?=[^\Wb]*b)  # Assert word contains a "b".
    \w+           # Match the word having both "a" and "b".
    \b            # Anchor to end of word.
    """, re.VERBOSE | re.IGNORECASE)
0
source

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


All Articles