Regular Expression Matching New Line

Reply

Join Date: Jun 2005
Posts: 2
Reputation: p1no is an unknown quantity at this point 
Solved Threads: 0
p1no p1no is offline Offline
Newbie Poster

Regular Expression Matching New Line

 
0
  #1
Jun 24th, 2005
I'm trying to add copyright information to the top of XML files. However, it needs to go after the prologue:

<?xml version="1.0"?>
<!DOCTYPE ...>

My problem is that some XML Documents have the <!DOCTYPE...> tag spread out over many lines and I need to add the copyright information after the whole tag. With the following regular expression, it only matches the first line of the <!DOCTYPE...> tag. Any help would be appriciated. Thanks.

  1. if($XML)
  2. {<blockquote> $holdTerminator = $/;
  3. undef $/;
  4. $buf = <DAT> or die "Can't read into variable";
  5. $/ = $holdTerminator;
  6. if($buf =~ m/(<\?xml version="\d\.\d".*\?>[.\s\n]*(<!DOCTYPE.*>?)?)/i)
  7. {</blockquote><blockquote> <blockquote>print "XML $1";
  8. $version=$1;
  9. $buf =~ s/<\?xml version="\d\.\d".*\?>[.\s\n]*(<!DOCTYPE.*>?)?/$version \n\n $start_comment $copyright $end_comment/i;
  10. seek(DAT, 0, 0);
  11. print DAT $buf;</blockquote></blockquote><blockquote>}</blockquote>}
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Regular Expression Matching New Line

 
0
  #2
Jun 24th, 2005
http://www1.cs.columbia.edu/~lennox/perlre.html is a great site to learn about regex's.... look up the "multi-line" regular expression test with the match operator. You'll be amazed at the power and versatility of it.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2
Reputation: p1no is an unknown quantity at this point 
Solved Threads: 0
p1no p1no is offline Offline
Newbie Poster

Re: Regular Expression Matching New Line

 
0
  #3
Jun 24th, 2005
Thanks for the reply. I had tried adding /m to the end of the substitution, but that didn't work. Then it matches the <?XML...> tag, but not the <!DOCTYPE...>. It inserts the copyright info inbetween the <?XML...> and <!DOCTYPE...> tags, like it didn't recognize the <!DOCTYPE...> tag. I also tried adding /s to the end, and that matched the entire document, which really screwed things up. The beginning of the XML file I'm testing looks like this...

[HTML]<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> [/HTML]

but there are a few other files that have many lines of <!DOCTYPE...>.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC