in reply to Building an XML File from text

I am thinking that this is not the best approach. The XML packages tend to be more for taking structured data and putting it in an XML document (ie databases or values of variables/hashes/arrays) To work with unstructured data, regular expressions seem like the best bet. After loading the text into a variable you do some subsitutes, numbers are easy
$text =~ s/^(|.*\s)(\d+)(\s.*|)$/$1<number>$2<\/number>$3/ # The expression looks like the following # beginning of line followed by either nothing or at least # one space which neighbors a set of digits followed by # # either nothing or at least a space and the end of the line
Deates would be done in a similar approach replacing \d+ with whatever a date looks like. I think there are some loose date modules you might can still some RE's from.
----
I always wanted to be somebody... I guess I should have been more specific.

Replies are listed 'Best First'.
Re: Re: Building an XML File from text
by pike (Monk) on Nov 20, 2001 at 20:25 UTC
    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

      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