| | |
Regular Expression Matching New Line
![]() |
•
•
Join Date: Jun 2005
Posts: 2
Reputation:
Solved Threads: 0
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.
<?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.
Perl Syntax (Toggle Plain Text)
if($XML) {<blockquote> $holdTerminator = $/; undef $/; $buf = <DAT> or die "Can't read into variable"; $/ = $holdTerminator; if($buf =~ m/(<\?xml version="\d\.\d".*\?>[.\s\n]*(<!DOCTYPE.*>?)?)/i) {</blockquote><blockquote> <blockquote>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;</blockquote></blockquote><blockquote>}</blockquote>}
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.
•
•
Join Date: Jun 2005
Posts: 2
Reputation:
Solved Threads: 0
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...>.
[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...>.
![]() |
Similar Threads
- regular expression (PHP)
- noobie asking for assistance with file parsing... (Perl)
- Regular expression (Perl)
- Looking for table rows with Regular expression. (PHP)
Other Threads in the Perl Forum
- Previous Thread: Failing with the as version and msi version could not be installed.
- Next Thread: parallel port access in perl
| Thread Tools | Search this Thread |






