|Keep It Simple, Stupid
How to strip comments and whitespace from a regex defined with /x?by jh (Beadle)
|on Jan 19, 2018 at 19:27 UTC
jh has asked for the wisdom of the Perl Monks concerning the following question:
I would like to print out some previously-defined regular expressions in a compact format. The main thing I want to do is strip out the whitespace and comments from those defined with the /x modifier. So if I have two functionally identical regexes:
then I'd like to make a function clean_regex such that
both print the same thing. Ideally clean_regex is a no-op on regexes defined without the /x modifier, such that
also prints the same thing. I have hacked together something gross and terrible but I was hoping for something better, presumably asking the Regexp compiler what it has once it's done throwing away comments and whitespace.