Search code examples
pythonregexpython-re

split string based on regular expression value in python


suppose i have a string

exp = '"security_datafilter"."PRODUCT_CATEGORIES"."CATEGORY_NAME" IN ("CPU","Storage")' 

I want to split the string based on word IN so my exprected result is

['"security_datafilter"."PRODUCT_CATEGORIES"."CATEGORY_NAME"','IN','("CPU","Storage")']

but in my case it doesnt work This is what i have tried

import re
exp_split = re.split(r'( in )',exp,re.I) 

Solution

  • re documentation: re.split(pattern, string, maxsplit=0, flags=0)

    The split() function expects that the third positional argument is the maxsplit argument. Your code gives re.I to maxsplit and no flags. You should give flags as a keyword argument like so:

    exp_split = re.split(r'( in )',exp, flags=re.I)