Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Wanted: Perl Regex Pretty Printer

by saintmike (Vicar)
on Aug 13, 2013 at 04:50 UTC ( #1049212=perlquestion: print w/replies, xml ) Need Help??

saintmike has asked for the wisdom of the Perl Monks concerning the following question:

Is there a pretty printer for Perl regexes that makes use of the /x switch and splits up a complicated regex over several lines to improve its readability?

Something like perltidy for regular expressions, except perltidy seems to leave them untouched even if they should be broken up into several lines.

Replies are listed 'Best First'.
Re: Wanted: Perl Regex Pretty Printer
by Happy-the-monk (Canon) on Aug 13, 2013 at 05:22 UTC

    Are you maybe looking for YAPE::Regex::Explain?

    By way of presenting its explanations it preaks up regexes
    and uses the /x-switch to do still have them execute.

    While it is not a "pretty printer", it has all that's needed in it's code.

    Other YAPE::Regex-modules may help, too.

    Cheers, Sören

    Créateur des bugs mobiles - let loose once, run everywhere.
    (hooked on the Perl Programming language)

Re: Wanted: Perl Regex Pretty Printer
by ambrus (Abbot) on Aug 13, 2013 at 09:00 UTC

    I don't know, but I for one don't really believe in syntax coloring or other tools that try to make any code automatically readable.

    As an example, take a look at the ugly regex in Decode LaTeX flying accents. The regex is broken to lines with the /x switch to make it readable, but that still doesn't help. The problem with that regex is that I wrote that way back before support of perl 5.10 was universally available, thus the regex does not use named captures, even though it would really need them. An automatic reformatting tool will not provide you meaningful names for the named captures.

Re: Wanted: Perl Regex Pretty Printer (almost, rxrx, rxplain)
by Anonymous Monk on Aug 13, 2013 at 07:28 UTC

    See Re: ppiwx / wxPPI / wxppixregexp xPPIx_Regexp_linecol_onize / PPIx::Regexp::Element::column_number

    Now I've coded up a replacement for YAPE::Regex::Explain, i'm close to posting it, see example at Re: regex help! (regexplain) -- it prettifies the easy way, without altering the original regex (it's eval-able)

    rxrx also provides a describe-the-regex thing , but it needs to run it, example

    Ignoring the ansi escapes failure on win32, its prettyfied, each () indents, literals are literals ... its a very short hand-editing step from that to qr{}x

    Compare to the original //x hand annotated

    $_=1234; m{ (?<P>(?&V)) # match <V> and save to $+{P} (?<Q> .) # match <Q> and save to $+{Q} # this can be saved in $v_definition = qr// (?(DEFINE) (?<V> ...) # <V> aka (?&V) is three chars ) }xm;

    A little search/replace and you have

    Oh, duh, here is rxplain output (some perldoc links are broken, todo)

    anyone considering consideration to change pre/to/code because of [] shouldn't because there ain't no issue

Re: Wanted: Perl Regex Pretty Printer
by ryanc (Monk) on Aug 15, 2013 at 18:10 UTC
    It isn't a pretty printer, but it sure does help with troubleshooting -- Damien's Regexp::Debugger.
Re: Wanted: Perl Regex Pretty Printer (emacs)
by LanX (Archbishop) on Nov 09, 2013 at 02:38 UTC
    FWIW if you use emacs in cperl-mode you get two built in commands to either expand the whole regex our only the group under the cursor:

    cperl-beautify-regexp

    cperl-beautify-regexp-piece

    Cheers Rolf

    ( addicted to the Perl Programming Language)

    PS: my writeup n 3000... =)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1049212]
Approved by Loops
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2020-05-27 23:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (162 votes). Check out past polls.

    Notices?