Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > help with regex

Reply
Thread Tools

help with regex

 
 
George Mpouras
Guest
Posts: n/a
 
      06-19-2013
I must discover all possible field names of a key/value file.
The properties of the file are unknown so I must be a little creative.
The values optional can have whitespaces inside "..."
its key/value separated with a space from the next pair.
Do you thing the following is ok ?



#!/usr/bin/perl
use strict;
use warnings;

while(<DATA>) { chomp;

while ( /([^=]+)=("[^"]+"|\S+)/g ) {
my ($key, $val) = ($1, $2);
$val =~s/^["\s]*(.*?)["\s]*$/$1/;
print "*$key* *$val*\n"
}

print "--------\n"
}


__DATA__
f1=hello f2= f3="foo" f4="hello world"
f6="day" f7="day & night" f8=100

 
Reply With Quote
 
 
 
 
Tim McDaniel
Guest
Posts: n/a
 
      06-20-2013
In article <kps6f4$15u3$(E-Mail Removed)>,
George Mpouras <(E-Mail Removed) m> wrote:
>I must discover all possible field names of a key/value file.
>The properties of the file are unknown


If by "properties" you mean the layout, format, et cetera,
then how can anyone advise you on a proper way to parse it
when neither you nor we know what's valid?

--
Tim McDaniel, http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Peter Gordon
Guest
Posts: n/a
 
      06-20-2013
George Mpouras <(E-Mail Removed) m> wrote
in news:kps6f4$15u3$(E-Mail Removed):

> I must discover all possible field names of a key/value file.
> The properties of the file are unknown so I must be a little creative.
> The values optional can have whitespaces inside "..."
> its key/value separated with a space from the next pair.
> Do you thing the following is ok ?
>
>
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> while(<DATA>) { chomp;
>
> while ( /([^=]+)=("[^"]+"|\S+)/g ) {
> my ($key, $val) = ($1, $2);
> $val =~s/^["\s]*(.*?)["\s]*$/$1/;
> print "*$key* *$val*\n"
> }
>
> print "--------\n"
> }
>
>
> __DATA__
> f1=hello f2= f3="foo" f4="hello world"
> f6="day" f7="day & night" f8=100
>
>


You don't say if all possible sequences are included in the data.
If they are, the code below decodes it.

#!/usr/bin/perl -w
use strict;
use 5.14.0;
my %lines;
while( <DATA> ) {
chomp;
last if /^$/; # Catch blank lines at end of data.
while ( /(f\d+)=(.*?)(?: f\d+=|$)/ ) {
my $key = $1;
my $value = $2;
s/$key=$value(.*)/$1/; # Strip the key/value pair off the
string.
$value =~ s/"(.*)"/$1/; # Strip off any "
$lines{$key} = $value;
}
}
say "The Hash";
foreach my $key (sort keys %lines ) {
say "$key: $lines{$key}";
}
__DATA__
f1=hello f2= f3="foo" f4="hello world"
f6="day" f7="day & night" f8=100

 
Reply With Quote
 
George Mpouras
Guest
Posts: n/a
 
      06-20-2013
key names can be whatever string with no spaces not f\d+
f100 etc was an example, so the regex
/(f\d+)=(.*?)(?: f\d+=|$)/ )
is not catching correctly


 
Reply With Quote
 
George Mpouras
Guest
Posts: n/a
 
      06-20-2013
key names can be whatever string with no spaces not only f\d+
 
Reply With Quote
 
George Mpouras
Guest
Posts: n/a
 
      06-20-2013
you are correct, specs are loosy
lines with multiple key/value pairs separated by space
keys are not containing space
values may contain space inside double quotes
 
Reply With Quote
 
Tim McDaniel
Guest
Posts: n/a
 
      06-20-2013
In article <kpvome$ck5$(E-Mail Removed)>,
George Mpouras <(E-Mail Removed) m.com.nospam> wrote:
>key names can be whatever string with no spaces not f\d+


The examples were the only spec you gave, so you can understand why
people coded to it.

>f100 etc was an example


An example provided by the teacher of the class?

Settings separated by space: do you mean one space or one or more
characters of whitespace?
Is there always an "="? That is, you can't have "foo bar"; it must be
"foo= bar="?
Can there be whitespace around "=", as in "foo = bar";
Can there be leading whitespace and/or trailing whitespace on the line?

--
Tim McDaniel, (E-Mail Removed)
 
Reply With Quote
 
George Mpouras
Guest
Posts: n/a
 
      06-20-2013

> An example provided by the teacher of the class?


!!!!!!!!!!!!!!!!!!!!!!!!!!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with detailsView insert sim18 ASP .Net Web Controls 0 05-07-2013 01:14 PM
Help with loading file into an array peter berrett Python 3 05-05-2013 11:44 AM
Getting an error with lambda and async - help interpret, please Victor Bazarov C++ 9 04-22-2013 03:24 PM
Help with printing a bit pattern with printf and %x matt.jaffe@gmail.com C Programming 9 04-19-2013 12:14 PM
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM



Advertisments