Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: Regex for removing Template::Toolkit comments?

by bliako (Monsignor)
on Aug 26, 2018 at 09:43 UTC ( [id://1221124]=note: print w/replies, xml ) Need Help??


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

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) ...

  • Comment on Re^2: Regex for removing Template::Toolkit comments?

Replies are listed 'Best First'.
Re^3: Regex for removing Template::Toolkit comments?
by tybalt89 (Monsignor) on Aug 26, 2018 at 14:11 UTC
    #!/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 %]

        Both

        comment nust be removed 9

        and

        comment nust be removed 10

        are incorrect, they should both stay under the rules you posted.

Log In?
Username:
Password:

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

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

    No recent polls found