-1

I'm trying to write a Python regex pattern to match anything with "male" but not "female" in it. A big issue for me is that the strings can look like this: "word_999male_word", so I can't just match "male" itself.

Here's my testfile I created so far:

import re

testlist = ['female', 'male',
            'female_word', 'male_word',
            'word_female', 'word_male',
            'word_female_word', 'word_male_word',
            'word_123female_word', 'word_999male_word']

# want to match everything that contains 'male' but not 'female'
for item in testlist:
    PATTERN = "male"
    PAT = re.compile(PATTERN)
    print(PAT.search, item)
khelwood
  • 52,115
  • 13
  • 74
  • 94
kkq804
  • 1

0 Answers0