Home: Perl Programming Help: Regular Expressions:
Trouble formulating substitute regular expression, need some help, please



poly4life
Novice

Apr 1, 2011, 7:24 AM


Views: 4957
Trouble formulating substitute regular expression, need some help, please

Hello,

I am having great difficulty putting together this regular expression and I am asking for help.

I have a file like this:

Code
<title>Example</title> 
<!--
This is a comment for this page. Make sure to fill this area with appropriate information.
-->
<style type="text/css">


I'm trying to make it looks like this:

Code
<title>Example</title> 
<!--
This is a comment for this page. Make sure to fill this area with appropriate information.
-->
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<style type="text/css">


Here's the substitution I'm using that is not working:


Code
$contents =~ s|(<title>.*</title>)(\S<!--[\s\S]*-->\S)(<style)|$1$2<meta http-equiv="Content-type" content="text/html; charset=utf-8" />\n$3|i;


Thank you.


miller
User

Apr 1, 2011, 1:10 PM


Views: 4929
Re: [poly4life] Trouble formulating substitute regular expression, need some help, please


Code
use strict; 

my $data = do {local $/; <DATA>};

my $str = qq{<meta http-equiv="Content-type" content="text/html; charset=utf-8" />};

$data =~ s{<title>.*?</title>\s*<!--.*?-->\s*\K}{$str\n}s;

print $data;

__DATA__
<title>Example</title>
<!--
This is a comment for this page. Make sure to fill this area with appropriate information.
-->
<style type="text/css">