Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Regex for removing Template::Toolkit comments?

by tybalt89 (Monsignor)
on Aug 24, 2018 at 21:05 UTC ( [id://1221063]=note: print w/replies, xml ) Need Help??


in reply to Regex for removing Template::Toolkit comments?

Here's a first try at a little recursive parser that will strip nested items.

If you have a better test case (or a counter-example) please let me know.

#!/usr/bin/perl # https://perlmonks.org/?node_id=1221039 use strict; use warnings; $_ = <<END; before [% # this is a comment to the end of line foo = 'bar' %] <p>bw, bliako</p> [%# placing the '#' immediately inside the directive tag comments out the entire directive %] [% outside %] [%# placing the '#' immediately inside the directive tag comments out the entire directive [% inside %] %] after END print stripcomments(); sub stripcomments { my $answer = ''; $answer .= /\G\[\%#/gc ? stripcomments() x 0 : /\G\[\%/gc ? '[%' . stripcomments() =~ s/#.*//gr . '%]' : /\G\%\]/gc ? return $answer : /\G./gcs ? $& : return $answer while 1; }

Outputs:

before [% foo = 'bar' %] <p>bw, bliako</p> [% outside %] after

Replies are listed 'Best First'.
Re^2: Regex for removing Template::Toolkit comments?
by bliako (Monsignor) on Aug 26, 2018 at 09:43 UTC

    tybalt89, giveth with one hand and taketh away with the other...

    I am struggling to convert it to a function with input parameter... Getting there (in a biblical sense) ...

      #!/usr/bin/perl # https://perlmonks.org/?node_id=1221039 use strict; use warnings; my $someTTstring = <<END; before [% # this is a comment to the end of line foo = 'bar' %] <p>bw, bliako</p> [%# placing the '#' immediately inside the directive tag comments out the entire directive %] [% outside %] [%# placing the '#' immediately inside the directive tag comments out the entire directive [% inside %] %] after END print stripcomments($someTTstring); sub stripcomments { @_ and local $_ = shift; my $answer = ''; $answer .= /\G\[\%#/gc ? stripcomments() x 0 : /\G\[\%/gc ? '[%' . stripcomments() =~ s/#.*//gr . '%]' : /\G\%\]/gc ? return $answer : /\G./gcs ? $& : return $answer while 1; }

      Like this?

        In my attempts I have omitted @_ and and was getting Use of uninitialized value $_, but forgotten about when you call it later on without any params. So, that works for multiline comments but not single line. I have put the following test input which I presume is valid Template::Toolkit comment syntax with the exception of nested comments which I am not sure. I will go the Parser way haukex posted. thanks

        [%# comment nust be removed 1 %] [% # comment nust be removed 2 must stay 0 %] [% # comment nust be removed 3 must stay 1 %] [% # comment nust be removed 4 must stay 2 # comment nust be removed 5 [%# nested comment (not sure if allowed or not) 1 %] %] [%# comment nust be removed 6 comment nust be removed 7 comment nust be removed 8 [%# nested comment (not sure if allowed or not) 2 %] %] [% # comment nust be removed 9 [% comment nust be removed 10 %] [% # nested comment (not sure if allowed or not) 2 %] %] [% must stay 3 %] [% must stay 4 must stay 5 # comment nust be removed 11 %] [% # comment nust be removed 12 must stay 6 %]

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-24 02:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found