regex - Is there a Python equivalent to the Perl "/x" modifier for regular expressions? -
perl makes easy build readable regular expressions using /x modifier. modifier allows write regular look strings , ignore whitespaces in these strings. in other words, logical parts of regular look can separated whitespace or carriage returns, allowing great readability. in python, way see of doing build such regular look string, remove whitespace in intermediate step, , utilize resulting string matching. there more elegant way of doing this?
yes, setting re.x / re.verbose flag:
this flag allows write regular expressions nicer. whitespace within pattern ignored, except when in character class or preceded unescaped backslash, and, when line contains '#' neither in character class or preceded unescaped backslash, characters leftmost such '#' through end of line ignored.
that means 2 next regular look objects match decimal number functionally equal:
a = re.compile(r"""\d + # integral part \. # decimal point \d * # fractional digits""", re.x) b = re.compile(r"\d+\.\d*") this pretty much /x perl flag.
python regex perl
No comments:
Post a Comment