CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



poly4life
Novice

Apr 1, 2011, 7:24 AM

Post #1 of 2 (2149 views)
Trouble formulating substitute regular expression, need some help, please Can't Post

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

Post #2 of 2 (2121 views)
Re: [poly4life] Trouble formulating substitute regular expression, need some help, please [In reply to] Can't Post


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">


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives