Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular expression 1

Status
Not open for further replies.

shlomyb

Programmer
Nov 14, 2006
32
IL
Which regular expression should I use to catch:
<?xml version="1.0" encoding="iso-8859-1"?>

When version can be "<number>.<number>"
And encoding can be some iso number ?
 
This might help:
Code:
my ($version,$encode);

if (m/<?xml\s+version="([^"]+)"\s+encoding="([^"]+)"/) {
    ($version,$encode) = ($1, $2);
    print "$version\|$encode\n";
} else {
    print "No match!";
}
If all your trying to get out of this is the xml version and encoding, by all means, use a regex. If you're trying to parse and entire XML document, that is a fairly non-trivial task that's better left to modules you can download from CPAN. XML::Simple may be of use, depending on how complex your XML structure is - or there are plenty of other modules for you to experiement with.
 
Thanks for your answer ....

What about the "?>" at the end ...
 
One small oversight with rharsh's regular expression. The question mark literal needs to be escaped:

Code:
my ($version,$encode);

if (m/<[COLOR=red]\[/color]?xml\s+version="([^"]+)"\s+encoding="([^"]+)"/) {
    ($version,$encode) = ($1, $2);
    print "$version\|$encode\n";
} else {
    print "No match!";
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top