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

Re: How to strip comments and whitespace from a regex defined with /x? (ppix_regexp_strip_comments)

by Anonymous Monk
on Apr 24, 2018 at 02:28 UTC ( [id://1213465]=note: print w/replies, xml ) Need Help??


in reply to How to strip comments and whitespace from a regex defined with /x?

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
  • Comment on Re: How to strip comments and whitespace from a regex defined with /x? (ppix_regexp_strip_comments)
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1213465]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2025-07-13 03:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.