F
fatted
I'd like to try and combine what I currently have in 4 regexps into
(maybe) one regexp.
I'm trying to parse data from a string, where the string is in the
format:
s_left *or* s_left = s_right.
rules:
s_right may have one or more equals in it.
In a
s_left = s_right
white space between the last/first non-white space char and the '='
char should be ignored (not part of s_left or s_right).
If s_left has a equals in it, s_left should be wrapped in double
quotes ("s_le=ft" = s_right)
Otherwise s_left is taken as beginning at the start of line, and
continuing up to the '=' char. (excluding white space between the last
non-white space from the sentence_left and the '=')
So, I have:
use warnings;
use strict;
# some test values
my @array = (
'"this = that"',
'log.log',
'start = "1"',
'sum=5+3=21',
'sql=SELECT * FROM table WHERE col17 = 2',
'"eq=als"=5=7',
'my name is = fatted',
'"1=1" = chicken'
);
foreach (@array)
{
unless( /=/ )
{
print "[zero] ".$_."\n";
}
elsif( /^"(.+)"$/ )
{
print "[one] ".$1."\n";
}
elsif( /^"([^"]*)[^=]*=\s*(.+)/ )
{
print "[two] ".$1."[]".$2."\n";
}
elsif( /^([^=]*)=\s*(.+)/ )
{
my $one = $1;
my $two = $2;
$one =~ s/\s+$//;
print "[three] ".$one."[]".$two."\n";
}
}
which gives:
[one] this = that
[zero] log.log
[three] start[]"1"
[three] sum[]5+3=21
[three] sql[]SELECT * FROM table WHERE col17 = 2
[two] eq=als[]5=7
[three] my name is[]fatted
[two] 1=1[]chicken
Which is what I require. But I'd now like to see if I could write the
regexp in one go, but I'm not having much luck, for instance:
one attempt to combine the last 2 regexps:
elsif( /^("|)([^"]*|[^=]*)[^=]*=\s*(.+)/)
{
# same processing as in last elsif block above
}
Doesn't work (not same results as above)
(maybe) one regexp.
I'm trying to parse data from a string, where the string is in the
format:
s_left *or* s_left = s_right.
rules:
s_right may have one or more equals in it.
In a
s_left = s_right
white space between the last/first non-white space char and the '='
char should be ignored (not part of s_left or s_right).
If s_left has a equals in it, s_left should be wrapped in double
quotes ("s_le=ft" = s_right)
Otherwise s_left is taken as beginning at the start of line, and
continuing up to the '=' char. (excluding white space between the last
non-white space from the sentence_left and the '=')
So, I have:
use warnings;
use strict;
# some test values
my @array = (
'"this = that"',
'log.log',
'start = "1"',
'sum=5+3=21',
'sql=SELECT * FROM table WHERE col17 = 2',
'"eq=als"=5=7',
'my name is = fatted',
'"1=1" = chicken'
);
foreach (@array)
{
unless( /=/ )
{
print "[zero] ".$_."\n";
}
elsif( /^"(.+)"$/ )
{
print "[one] ".$1."\n";
}
elsif( /^"([^"]*)[^=]*=\s*(.+)/ )
{
print "[two] ".$1."[]".$2."\n";
}
elsif( /^([^=]*)=\s*(.+)/ )
{
my $one = $1;
my $two = $2;
$one =~ s/\s+$//;
print "[three] ".$one."[]".$two."\n";
}
}
which gives:
[one] this = that
[zero] log.log
[three] start[]"1"
[three] sum[]5+3=21
[three] sql[]SELECT * FROM table WHERE col17 = 2
[two] eq=als[]5=7
[three] my name is[]fatted
[two] 1=1[]chicken
Which is what I require. But I'd now like to see if I could write the
regexp in one go, but I'm not having much luck, for instance:
one attempt to combine the last 2 regexps:
elsif( /^("|)([^"]*|[^=]*)[^=]*=\s*(.+)/)
{
# same processing as in last elsif block above
}
Doesn't work (not same results as above)