3

I'm trying to make an automated build and use Perl in it to update some paths in a file.

Specifically, in an html file, I want to take the block shown below

<!-- BEGIN: -->
<script src="js/a.js"></script>
<script src="js/b.js"></script>
<script src="js/c.js"></script>
<!-- END: -->

and replace it with

<script src="js/all.js"></script>


I have tried a few regexes like:

perl -i -pe  's/<--BEGIN:(.|\n|\r)*:END-->/stuff/g' file.html

or just starting with:

perl -i -pe  's/BEGIN:(.|\n|\r)*/stuff/g' file.html

But I can't seem to get past the first line. Any ideas?

1 Answer 1

2
perl -i -pe 's/<--BEGIN:(.|\n|\r)*:END-->/stuff/g' file.html

This is so close.

  • Now just match with the /s modifier, this allows . to match any char, including newlines.
  • Most importantly, you want to start the match with <!--, note the !.
  • Also, you want a non-greedy match like .*?, in case you have multiple END markers.
  • Your example input shows that there may be extra spaces.

This would lead to the following substitution:

s/<!--\s*BEGIN:.*?END:\s*-->/stuff/sg

As @plusplus pointed out, the -p iterates over each line. Let's change Perl's concept of a “line” to “the whole file at once”:

BEGIN { $/ = undef }

or use the -0 command line switch, without a numeric argument.

Sign up to request clarification or add additional context in comments.

5 Comments

does that work for you? perl -p is running over the file line by line (while (<>) etc) so it seems unlikely to match like this...?
Thanks! The above regex works beautifully if I put all the above html on one line - all the text is replaced with "stuff". If I separate the html into multi lines, it fails to match. I also tried removing the "-p". Hmmm now I remember why I do my utmost to avoid regex :)
see the edit above - amon's original post was untested and didn't work.
Oow sounds fun so would this look something like: <code>perl -i -e 'BEGIN { $/ = undef } s/<!--\sBEGIN:.?END:\s*-->/stuff/sg' file.html</code> must confess I haven't touched Perl in ages.
perl -0 -i -pe 's/<!--\s*BEGIN:.*?END:\s*-->/stuff/sg' file.html (PS - it's backticks to highlight as code in comments)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.