in reply to Re: Re: Building an XML File from text
in thread Building an XML File from text
You can still use regexps actually, you just have to "neutralize" the dates once you've marked them, by turning them into some kind of xml entity for example.
Here is the code:
#!/bin/perl -w use strict; # create the regexp for date, this should be improved my $month = qr/(?:(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\ +.?)/; my $day = qr/(?:(?:[0-2]?[0-9]|30|31)(?:st|nd|th)?)/; my $year = qr/(?:,?\s*\d+)?/; my $date= qr/($month $day$year)/; # this could probably be improved too! my $number= qr/(\d{2,})/; my %replace; my $i=0; while( <DATA>) { chomp; # entitize special characters, those 2 are sufficient here s{&}{&}g; s{<}{<}g; s{(?!<&)$date} {$i++; $replace{$i}="<date>$1</date>"; "&$i;"}eg; + # replace the dates s{(?!<&)$number}{$i++; $replace{$i}="<number>$1</number>"; "&$i;"} +eg; # replace the numbers s{&(\d+);}{$replace{$1}}g; + # replace the &n; print "<mytext>$_</mytext>\n"; } __DATA__ On Oct. 21, the Dow Jones rose to 10043 points On May 1st 2001, 12 people were working in France On Nov. 20, I can still tell that 123 < 234
|
|---|