As a followup to Re^3: How to strip comments and whitespace from a regex defined with /x?
#!/usr/bin/perl -- use strict; use warnings; use PPIx::Regexp; use Data::Dump; my $r = PPIx::Regexp->new(q{ s{ (?# probulary ) \d+ # digits [ ] # space \s+ # spaces \w+ # words (?# probulary )}{}gx }); print "\n\n", $r->content, "\n"; dd( $r ); delete_commentsss( $r ); print "\n\n"; dd($r); print "\n\n", $r->content, "\n"; sub delete_commentsss { my( $n ) = @_; for my $child ( eval { $n->children } ){ if( eval{ $child->children } ){ #$haskids delete_commentsss( $child ); } else { for my $il ( qw{ start children finish } ){ if( $n->{$il} ){ @{ $n->{$il} } = grep { not( $_->isa( "PPIx::Regexp::Token::Whitespace +" ) or $_->isa( "PPIx::Regexp::Token::Comment" ) ) } @{ $n->{$il} }; } } } } return $n; } __END__ s{ (?# probulary ) \d+ # digits [ ] # space \s+ # spaces \w+ # words (?# probulary )}{}gx bless({ children => [ bless({ content => "\n", perl_version_introduced => "5.000" }, "PP +Ix::Regexp::Token::Whitespace"), bless({ content => "s" }, "PPIx::Regexp::Token::Structure"), bless({ children => [ bless({ content => "\\d" }, "PPIx::Regexp::Token::CharClass::S +imple"), bless({ content => "+" }, "PPIx::Regexp::Token::Quantifier"), bless({ content => " ", perl_version_introduced => "5.000" }, +"PPIx::Regexp::Token::Whitespace"), bless({ content => "# digits\n" }, "PPIx::Regexp::Token::Comme +nt"), bless({ children => [bless({ content => " " }, "PPIx::Regexp::Token: +:Literal")], finish => [bless({ content => "]" }, "PPIx::Regexp::Token: +:Structure")], start => [bless({ content => "[" }, "PPIx::Regexp::Token: +:Structure")], type => [], }, "PPIx::Regexp::Structure::CharClass"), bless({ content => " ", perl_version_introduced => "5.000" }, +"PPIx::Regexp::Token::Whitespace"), bless({ content => "# space\n" }, "PPIx::Regexp::Token::Commen +t"), bless({ content => "\\s" }, "PPIx::Regexp::Token::CharClass::S +imple"), bless({ content => "+" }, "PPIx::Regexp::Token::Quantifier"), bless({ content => " ", perl_version_introduced => "5.000" }, +"PPIx::Regexp::Token::Whitespace"), bless({ content => "# spaces\n" }, "PPIx::Regexp::Token::Comme +nt"), bless({ content => "\\w" }, "PPIx::Regexp::Token::CharClass::S +imple"), bless({ content => "+" }, "PPIx::Regexp::Token::Quantifier"), bless({ content => " ", perl_version_introduced => "5.000" }, +"PPIx::Regexp::Token::Whitespace"), bless({ content => "# words \n" }, "PPIx::Regexp::Token::Comme +nt"), bless({ content => "(?# probulary )" }, "PPIx::Regexp::Token:: +Comment"), ], finish => [bless({ content => "}" }, "PPIx::Regexp::Token::Delim +iter")], max_capture_number => 0, start => [ bless({ content => "{" }, "PPIx::Regexp::Token::Delimiter"), bless({ content => "\n", perl_version_introduced => "5.000" }, + "PPIx::Regexp::Token::Whitespace"), bless({ content => "(?# probulary )" }, "PPIx::Regexp::Token:: +Comment"), bless({ content => "\n", perl_version_introduced => "5.000" }, + "PPIx::Regexp::Token::Whitespace"), ], type => [], }, "PPIx::Regexp::Structure::Regexp"), bless({ content => "{" }, "PPIx::Regexp::Token::Delimiter"), bless({ content => "}" }, "PPIx::Regexp::Token::Delimiter"), bless({ content => "gx", modifiers => { g => 1, x => 1 } }, "PPIx: +:Regexp::Token::Modifier"), bless({ content => "\n", perl_version_introduced => "5.000" }, "PP +Ix::Regexp::Token::Whitespace"), ], effective_modifiers => { g => 1, x => 1 }, failures => 0, source => "\ns{\n(?# probulary )\n\\d+ # digits\n[ ] # space\n\\s+ # + spaces\n\\w+ # words \n(?# probulary )}{}gx\n", }, "PPIx::Regexp") bless({ children => [ bless({ content => "s" }, "PPIx::Regexp::Token::Structure"), bless({ children => [ bless({ content => "\\d" }, "PPIx::Regexp::Token::CharClass::S +imple"), bless({ content => "+" }, "PPIx::Regexp::Token::Quantifier"), bless({ children => [bless({ content => " " }, "PPIx::Regexp::Token: +:Literal")], finish => [bless({ content => "]" }, "PPIx::Regexp::Token: +:Structure")], start => [bless({ content => "[" }, "PPIx::Regexp::Token: +:Structure")], type => [], }, "PPIx::Regexp::Structure::CharClass"), bless({ content => "\\s" }, "PPIx::Regexp::Token::CharClass::S +imple"), bless({ content => "+" }, "PPIx::Regexp::Token::Quantifier"), bless({ content => "\\w" }, "PPIx::Regexp::Token::CharClass::S +imple"), bless({ content => "+" }, "PPIx::Regexp::Token::Quantifier"), ], finish => [bless({ content => "}" }, "PPIx::Regexp::Token::Delim +iter")], max_capture_number => 0, start => [bless({ content => "{" }, "PPIx::Regexp::Token::Delimi +ter")], type => [], }, "PPIx::Regexp::Structure::Regexp"), bless({ content => "{" }, "PPIx::Regexp::Token::Delimiter"), bless({ content => "}" }, "PPIx::Regexp::Token::Delimiter"), bless({ content => "gx", modifiers => { g => 1, x => 1 } }, "PPIx: +:Regexp::Token::Modifier"), ], effective_modifiers => { g => 1, x => 1 }, failures => 0, source => "\ns{\n(?# probulary )\n\\d+ # digits\n[ ] # space\n\\s+ # + spaces\n\\w+ # words \n(?# probulary )}{}gx\n", }, "PPIx::Regexp") s{\d+[ ]\s+\w+}{}gx

In reply to Re: How to strip comments and whitespace from a regex defined with /x? (ppix_regexp_strip_comments) by Anonymous Monk
in thread How to strip comments and whitespace from a regex defined with /x? by jh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.