in reply to Re: Building an XML File from text
in thread Building an XML File from text

The reason why I don't want to use regexes here is that things like '23' can occur within a date or as 'bare' number. If I insert markup tags as you propose here, I will end up with <date>Oct. <number>23</number></date> rather than <date>Oct. 23</date>.

pike

Replies are listed 'Best First'.
Re: Re: Re: Building an XML File from text
by mirod (Canon) on Nov 20, 2001 at 21:04 UTC

    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{&}{&amp;}g; s{<}{&lt;}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