Perl300 has asked for the wisdom of the Perl Monks concerning the following question:
Test.txt is the file I am trying to convert into xml and following are the contents of Test.txt
GI-eSTB-MIB-NPH::eSTBUserSettingOutput.0 = INTEGER: hd1920x1080i(1) GI-eSTB-MIB-NPH::eSTBUserSetting43OverRide.0 = INTEGER: on480p(3)
The code I am using is:
#!/usr/bin/perl use strict; use warnings; use XML::Writer; my $out; my $xml = XML::Writer->new(OUTPUT => \$out, DATA_MODE => 1, DATA_INDEN +T => ' '); $xml->xmlDecl(); $xml->startTag('doc'); my $check_1 = 0; open(my $fh, "<", "/home/drone/DrumTesting/Test.txt") or die "Failed to open file: $!\n"; while(<$fh>) { chomp; next if !length; my ($string1, $string2, $subscript_name, $subscript_value) = / ^([^\::]+) ([^\s]+) \.([^\s]+) \s(.*) /x; if ( $check_1 == 0 ) { $xml->startTag($string1); $check_1 += 1; } $xml->startTag($string2); $xml->dataElement($subscript_name => $subscript_value); $xml->endTag(); } $xml->endTag(); $xml->endTag(); $xml->end(); print $out; close $fh;
This generates following xml
<?xml version="1.0"?> <doc> <GI-eSTB-MIB-NPH> <::eSTBUserSettingOutput> <0>= INTEGER: hd1920x1080i(1)</0> </::eSTBUserSettingOutput> <::eSTBUserSetting43OverRide> <0>= INTEGER: on480p(3)</0> </::eSTBUserSetting43OverRide> </GI-eSTB-MIB-NPH> </doc>
What I am trying to do now is get rid of leading "::" and "= " from tags. Can you please suggest me what regex changes will I have to do so $string2 and $subscript_value would start from the position I want instead of where the preceding variable stopped fetching. I think it should be something like:
^([^\::]+) (/::(\[a-zA-Z]+)\./) \.([^\s]+) \s(.*)
Instead of what I have used in code, but it is showing error after i compile it: Unmatched ( in regex; marked by <-- HERE in m/ ^(^\::+) ( <-- HERE / at <script_name>.pl line 24.
Can you please suggest me what regex changes will I have to do so $string2 and $subscript_value would start from the position I want instead of where the preceding variable stopped fetching?
Also please suggest me some good place where I can learn and practice regex extensively
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: What regex changes are needed while creating xml
by toolic (Bishop) on Jun 25, 2015 at 18:12 UTC | |
|
Re: What regex changes are needed while creating xml
by stevieb (Canon) on Jun 25, 2015 at 18:24 UTC | |
by Perl300 (Friar) on Jun 25, 2015 at 19:21 UTC | |
by Perl300 (Friar) on Jun 25, 2015 at 21:37 UTC | |
by hippo (Archbishop) on Jun 25, 2015 at 22:56 UTC | |
by stevieb (Canon) on Jun 25, 2015 at 21:43 UTC |