Regexp help

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)
 
B

Brian McCauley

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,

foreach (@array)
{
if ( my ( $left,$right) = /^(\"[^\"]+\"|[^\"][^=]+?)\s*(?:=\s*(.*)\s*)?$/ ) {
$left =~ s/^"(.*)"$/$1/;
if ( defined $right ) {
print "[two] $left\[]$right\n";
} else {
print "[one] $left\n";
}
}else {
# Invalid input!
print "[zero] $_\n";
}
}

Produeces:

[one] this = that
[one] log.log
[two] start[]"1"
[two] sum[]5+3=21
[two] sql[]SELECT * FROM table WHERE col17 = 2
[two] eq=als[]5=7
[two] my name is[]fatted
[two] 1=1[]chicken

Note: double quote does not need to be backslashed in // but it's a
courtesy to do so for the beniefit of people using broken
auto-indenters.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
A

Anno Siegel

Brian McCauley said:
Note: double quote does not need to be backslashed in // but it's a
courtesy to do so for the beniefit of people using broken
auto-indenters.

I object.

All auto-indenters, syntax-hilighters and their ilk are broken in some
respect. ("Only perl can parse Perl") If we start accommodating their
glitches, we'll end up with coding conventions that are completely
irrational from a Perl point of view.

Anno
 
G

Greg Bacon

: [...] If we start accommodating their glitches, we'll end up with
: coding conventions that are completely irrational from a Perl point
: of view.

No kidding. What about the courtesy of refraining from using broken
autoindenters for benefit of people who know Perl? :)

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

help with regexp 5
Python battle game help 2
Help in this program. 2
HELP PLEASE 4
Function Help 1
Please help 2
Help with C negative indexes 2
regexp 9

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,840
Latest member
BrendanG78

Latest Threads

Top