How do I use asterisk in lookbehind regex in python? -
my test string:
1. default, no hair, w/o glass 2. ski suit 3. swim suit
how detect if there "no" or "w/o" before hair (there more 1 spaces in between)?
the final goal:
1. default, no hair, w/o glass returns false 1. default, no hair, w/o glass returns false 1. default, w/o hair, w/o glass returns false 1. default, w hair, w/o glass returns true
the goal tell whether glass should used or not.
my attempt: (?<!no\s)hair
(http://rubular.com/r/pdkbmyxpgh)
you can see in above example, if there more 1 space, regex won't work.
the re
module not support variable length (zero width) look-behind.
you need either:
fixed number of spaces before
hair
use
regex
module
short function using negative lookahead:
def re_check(s): return re.search(r'^[^,]+,\s+(?!(?:no|w/o)\s+hair,)', s) not none >>> re_check('default, no hair, w/o glass') false >>> re_check('default, w/o hair, w/o glass') false >>> re_check('default, w hair, w/o glass') true
Comments
Post a Comment