954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Regular Expression Matching New Line

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

My problem is that some XML Documents have the ..> 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 ..> tag. Any help would be appriciated. Thanks.

if($XML)
{[INDENT] $holdTerminator = $/;
   undef $/; 
   $buf = <DAT> or die "Can't read into variable";
   $/ = $holdTerminator;
   if($buf =~ m/(<\?xml version="\d\.\d".*\?>[.\s\n]*(<!DOCTYPE.*>?)?)/i)
     {[/INDENT][INDENT]	[INDENT]print "XML $1";
	$version=$1;
	$buf =~ s/<\?xml version="\d\.\d".*\?>[.\s\n]*(<!DOCTYPE.*>?)?/$version \n\n $start_comment $copyright $end_comment/i;
	seek(DAT, 0, 0);
	print DAT $buf;[/INDENT][/INDENT][INDENT]}[/INDENT]}
p1no
Newbie Poster
2 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

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.

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

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 ..>. It inserts the copyright info inbetween the <?XML...> and ..> tags, like it didn't recognize the ..> 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] [/HTML]

but there are a few other files that have many lines of ..>.

p1no
Newbie Poster
2 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You