GraphViz2::Parse::Regexp - Visualize a Perl regular expression as a graph


    use GraphViz2::Parse::Regexp;
    # no objects - quicker
    my $gd = GraphViz2::Parse::Regexp::to_graph('(([abcd0-9])|(foo))');

    # populate a GraphViz2 object with a Graph::Directed of a regexp
    my $gv = GraphViz2->from_graph(GraphViz2::Parse::Regexp::graphvizify($gd));

    # OO interface, using lazy-built attributes
    my $gvre = GraphViz2::Parse::Regexp->new(regexp => $regexp);
    my $gd = $gvre->as_graph; # Graph::Directed object
    # or supply a suitable Graph::Directed object
    my $gvre = GraphViz2::Parse::Regexp->new(as_graph => $gd);
    # then get the GraphViz2 object
    my $gv = $gvre->graph;

    # DEPRECATED ways to get $gvre with populated $gv
    my $gvre = GraphViz2::Parse::Regexp->new;
    $gvre->create(regexp => '(([abcd0-9])|(foo))');
    my $gv = $gvre->graph;
    # or give it a pre-set-up GraphViz2 object
    my $gv = GraphViz2->new(...);
    my $gvre = GraphViz2::Parse::Regexp->new(graph => $gv);
    # call ->create as above

    # produce a visualisation
    my $format = shift || 'svg';
    my $output_file = shift || "output.$format";
    $gv->run(format => $format, output_file => $output_file);

See t/gen.parse.regexp.t.


Takes a Perl regular expression and converts it into a Graph::Directed object, or directly into a GraphViz2 object.


This is the recommended interface.


    my $gd = GraphViz2::Parse::Regexp::to_graph('(([abcd0-9])|(foo))');

Given a Perl regular expression, returns a Graph::Directed object describing the finite state machine for it.


    my $gv = GraphViz2->from_graph(GraphViz2::Parse::Regexp::graphvizify($gd));

Mutates the given graph object to add to it the graphviz attributes visualisation "hints" that will make the "from_graph" in GraphViz2 method visualise this regular expression in the most meaningful way, including labels and groupings.

It is idempotent as it simply sets the graphviz attribute of the relevant graph entities.

Returns the graph object for convenience.


This is a Moo class, but with a recommended functional interface.

Constructor attributes


The regular expression to use.

This key is optional. You need to provide it by the time you access either the "as_graph" or "graph".


The Graph::Directed object to use. If not given, will be lazily built on access, from the "regexp".


The GraphViz2 object to use. This allows you to configure it as desired.

This key is optional. If provided, the create method will populate it. If not, it will have these defaults, lazy-built and populated from the "as_graph".

    my $gv = GraphViz2->new(
            edge   => {color => 'grey'},
            global => {directed => 1},
            graph  => {rankdir => 'TB'},
            node   => {color => 'blue', shape => 'oval'},

create(regexp => $regexp)

DEPRECATED. Mutates the object to set the regexp attribute, then accesses the as_graph attribute (possibly lazy-building it), then graphvizifys its as_graph attribute with that information, then from_graphs its graph.

Returns $self for method chaining.


Many thanks are due to the people who chose to make Graphviz Open Source.

And thanks to Leon Brocard, who wrote GraphViz, and kindly gave me co-maint of the module.


GraphViz2 was written by Ron Savage <> in 2011.

Home page:


Australian copyright (c) 2011, Ron Savage.

All Programs of mine are 'OSI Certified Open Source Software'; you can redistribute them and/or modify them under the terms of The Perl License, a copy of which is available at: