I want to write a regex that orders python to return items in a list that have sequence of vowels, defined by len=2
.
>>> chars = "aeiou"
>>> len = 2
>>> regex = re.compile(r"[+{}+]{{len}}",format(chars))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/re.py", line 234, in compile
return _compile(pattern, flags)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/re.py", line 286, in _compile
p = sre_compile.compile(pattern, flags)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/sre_compile.py", line 764, in compile
p = sre_parse.parse(p, flags)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/sre_parse.py", line 930, in parse
p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
TypeError: unsupported operand type(s) for &: 'str' and 'int'
>>>
>>> def funct(regex,list):
... for item in list:
... if regex.search(item):
... print(item)
...
>>> list = ['avid','Chaos','st','Cy']
>>>
>>> funct(regex,list)
avid
Chaos
I should be only getting Chaos
, not avid
. I'm having trouble understanding inputting len
parameter into the re.compile
module.