'Is there a general regex method can combine those conditions?

In my code, I use four four regex conditions but it is too long, so is there simple or general method can combine these conditions? I tried to r'[(abc|def)+^((?!(xyz|efg)).)*$^.*[^,|.]$]{1,10}$'but it didn't work...

s_l = ['abcabcabcabc', 'defdef.', 'sssssss', 'def', 'def,', 'xyzabc,', 'efgdefefg']

for idx, str_item in enumerate(s_l):
    if (re.match(r'(abc|def)+', str_item)  # find abc or def in str
    and re.match(r'^((?!(xyz|efg)).)*$', str_item)  # find xyz and efg not in str 
    and re.match(r'^.*[^,|.]$', str_item)   # comma not in the end of str
    and re.match(r'^[a-zA-Z]{1,10}$', str_item)):  # find length of str smaller than 10
        print(idx, "True")
    else:
        print(idx, "False")


Solution 1:[1]

You can use

import re
 
s_l = ['abcabcabcabc','defdef.','sssssss','def','def,','xyzabc,','efgdefefg']
 
for idx, str_item in enumerate(s_l):
    if re.match(r'^(?=abc|def)(?!.*(?:xyz|efg)).{1,10}$(?<![,.])', str_item): #find length of str smaller than 10
        print(idx, "True")
    else:
        print(idx, "False")

See the Python demo and the regex demo.

Details:

  • ^ - start of string
  • (?=abc|def) - no abc or def at the start is allowed
  • (?!.*(?:xyz|efg)) - neither xyz nor efg is allowed after any zero or more chars other than line break chars as many as possible
  • .{1,10} - one to ten chars other than line break chars
  • $ - end of string
  • (?<![,.]) - no , nor . allowed at the end of string.

Solution 2:[2]

You can simplify the conditional expression quite a bit.

regexes = (r'(abc|def)+', r'^((?!(xyz|efg)).)*$', r'^.*[^,|.]$', r'^[a-zA-Z]{1,10}$')
for idx, str_item in enumerate(s_l):
    if all(re.match(r, str_item) for r in regexes):
        print(idx, "True")
    else:
        print(idx, "False")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Wiktor Stribiżew
Solution 2 LeopardShark