Home: Perl Programming Help: Regular Expressions:
Help with an if regx statement



seahorse
Novice

Jan 30, 2007, 8:32 AM


Views: 24606
Help with an if regx statement

Please help me figure out how to write the 2nd part of the 2nd If statement below that I have written in plain english. The $in files represent examples of what is good input and what is bad input. Thanks in advance.

$in-bad1 = "[0],[0,1],[1,0,3,5]";

$in-bad2 = "[[0],[0,2]]";

$in-bad3 = "[[0]stuff,morestuff[0,4]]";

$in-good1 = "[0][0,1,2][0,1]";

$in-good2 = "[0]stuff[0,1][1,0,2,5]";

if ($in-bad1 =~ /\,/)) {


If there are , (comma) character(s) then they all must be located between a set of [ ] (bracket) characters.


}



KevinR
Veteran


Jan 30, 2007, 11:26 AM


Views: 24602
Re: [seahorse] Help with an if regx statement


Code
if ($line =~ /\],\[/) { 
print "it's bad";
}

-------------------------------------------------


seahorse
Novice

Jan 30, 2007, 1:27 PM


Views: 24599
Re: [KevinR] Help with an if regx statement

Thanks for your respone KevinR, however $inbad3 fails (says it's good when it should be bad). Below is a better version of the program thus far so please ignore the 1st one. Set $test to any of the $in strings to see what is good or bad. What I have labeled 'stuff' and 'morestuff' can be any number of characters except [ or ] or , (comma) characters.

Also, another rule is that the $in strings cannot be modified to 'fix' the problem.

$inbad1 = "[0],[0,1],[1,0,3,5]";

$inbad2 = "[[0],[0,2]]";

$inbad3 = "[[0]stuff,morestuff[0,4]]";

$ingood1 = "[0][0,1,2][0,1]";

$ingood2 = "[0]stuff[0,1][1,0,2,5]";

$test = $inbad3;

if ($test =~ /\,/) {

# If there are , (comma) character(s) then they all must be located between a set of [ ] (bracket) characters.


if ($test =~ /\],\[/) { print "it's bad"; } else { print "it's good"; }


}



(This post was edited by seahorse on Jan 30, 2007, 1:32 PM)


ProBulletin
Novice

Jan 30, 2007, 1:53 PM


Views: 24594
Re: [seahorse] Help with an if regx statement

There are better ways than this but it is one way at least. Unless I've overlooked something simple it was trickier than it first appeared.


Code
   
my $open = 0;
LOOP: while ($test =~ /(.)/g) {
my $char = $1;
if ($char eq '[' or $char eq ']') {
$open = $char eq '[' ? 1 : 0;
}
else {
if ($char eq ',' and $open == 0) {
print "INVALID";
last LOOP;
}
}
}



Regards,
Paul Wilson
ProBulletin Board: http://www.probulletin.com/


(This post was edited by ProBulletin on Jan 30, 2007, 1:53 PM)


seahorse
Novice

Jan 30, 2007, 2:30 PM


Views: 24590
Re: [ProBulletin] Help with an if regx statement

Kudos ProBulletin! It seems that you nearly got it. However, it fails the following case.

$inbad6 = "[0]stuff[0,1][1,0,2,5][stuff,morestuff[";

Is there a quick fix for that case?
Sorry about the confusion on stuff and morestuff. I know I wasn't real clear on what I wanted with them.


(This post was edited by seahorse on Jan 30, 2007, 3:45 PM)


KevinR
Veteran


Jan 30, 2007, 2:51 PM


Views: 24588
Re: [seahorse] Help with an if regx statement

"stuff" and "morestuff" is hardly a good description for coming up with pattern matches. Wink


Code
$inbad1 = "[0],[0,1],[1,0,3,5]"; 

$inbad2 = "[[0],[0,2]]";

$inbad3 = "[[0]stuff,morestuff[0,4]]";

$ingood1 = "[0][0,1,2][0,1]";

$ingood2 = "[0]stuff[0,1][1,0,2,5]";

$test = $inbad3;

for ($inbad1,$inbad2,$inbad3,$ingood1,$ingood2) {
if (/\](\w*,\w*)+\[/) { print "$_: it's bad\n"; } else { print "$_: it's good\n"; }
}

-------------------------------------------------


seahorse
Novice

Jan 30, 2007, 3:30 PM


Views: 24584
Re: [KevinR] Help with an if regx statement

Thanks for the 1-liner. Sorry about the confusion on stuff and morestuff. I know I wasn't real clear on what I wanted with them. Yours is close however there are still some cases where it fails, namely $inbad4 & $inbad5. Then there is the comma at the end and the [stuff,morestuff[ case. How would you fit these into your 1-liner? BTW, I also found ProBulletin's solution fails $inbad6

$inbad6 = "[0]stuff[0,1][1,0,2,5][stuff,morestuff[";




$inbad1 = "[0],[0,1],[1,0,3,5]";

$inbad2 = "[[0],[0,2]]";

$inbad3 = "[[0]stuff,morestuff[0,4]]";

$inbad4 = ",[0][0,1,2][0,1]";

$inbad5 = "[0]stuff[0,1][1,0,2,5]]stuff,morestuff]";

$ingood1 = "[0][0,1,2][0,1]";

$ingood2 = "[0]stuff[0,1][1,0,2,5]";

for ($inbad1,$inbad2,$inbad3,$inbad4,$inbad5,$ingood1,$ingood2) {


if (/\](\w*,\w*)+\[/) { print "$_: it's bad\n"; } else { print "$_: it's good\n"; }


}



(This post was edited by seahorse on Jan 30, 2007, 3:44 PM)


KevinR
Veteran


Jan 30, 2007, 4:49 PM


Views: 24579
Re: [seahorse] Help with an if regx statement

I give up.Crazy
-------------------------------------------------


ProBulletin
Novice

Jan 31, 2007, 2:39 AM


Views: 24571
Re: [seahorse] Help with an if regx statement

Try:


Code
 my $open = 0;      
my $comma = 0;
LOOP: while ($test =~ /(.)/g) {
my $char = $1;
if ($char eq '[' or $char eq ']') {
$open = $char eq '[' ? 1 : 0;
$comma = 0 if ($char = ']');
}
else {
if ($char eq ',' and $open == 0) {
print "INVALID";
last LOOP;
}
elsif ($char eq ',' and $open == 1) {
$comma = 1;
}
}
}
print "INVALID" if ($open and $comma);



Regards,
Paul Wilson
ProBulletin Board: http://www.probulletin.com/


(This post was edited by ProBulletin on Feb 1, 2007, 5:10 PM)


KevinR
Veteran


Jan 31, 2007, 4:10 PM


Views: 24562
Re: [seahorse] Help with an if regx statement

another approach:


Code
$s1 = "[0],[0,1],[1,0,3,5]"; 
$s2 = "[[0],[0,2]]";
$s3 = "[[0]stuff,morestuff[0,4]]";
$s4 = ",[0][0,1,2][0,1]";
$s5 = "[0]stuff[0,1][1,0,2,5]]stuff,morestuff]";
$s6 = "[0]stuff[0,1][1,0,2,5][stuff,morestuff[";
$s7 = "[0][0,1,2][0,1]";
$s8 = "[0]stuff[0,1][1,0,2,5]";
my $n = 1;
for ($s1,$s2,$s3,$s4,$s5,$s6,$s7,$s8) {
my $r = $_;
s/\[[^\]]*\]//g;
my $p = (/,/)? BAD: GOOD;
print "string \$s$n) $p: $r\n";
$n++;
}


output:

string $s1) BAD: [0],[0,1],[1,0,3,5]
string $s2) BAD: [[0],[0,2]]
string $s3) BAD: [[0]stuff,morestuff[0,4]]
string $s4) BAD: ,[0][0,1,2][0,1]
string $s5) BAD: [0]stuff[0,1][1,0,2,5]]stuff,morestuff]
string $s6) BAD: [0]stuff[0,1][1,0,2,5][stuff,morestuff[
string $s7) GOOD: [0][0,1,2][0,1]
string $s8) GOOD: [0]stuff[0,1][1,0,2,5]

Code
 
but if the occurances and frequencies of [] can be anywhere in the string in any combination this might prove to be less than 100% accurate. Pauls suggestion above might work better in that case.
-------------------------------------------------


ProBulletin
Novice

Jan 31, 2007, 4:15 PM


Views: 24560
Re: [KevinR] Help with an if regx statement

Just a minor point, but I would use index() instead of (/,/)


Regards,
Paul Wilson
ProBulletin Board: http://www.probulletin.com/


KevinR
Veteran


Jan 31, 2007, 11:04 PM


Views: 24557
Re: [ProBulletin] Help with an if regx statement

using index() is a good suggestion. Cool

edit:

BTW, nice to have another person take interest in this forum and post replies.

Regards,
Kevin
-------------------------------------------------


(This post was edited by KevinR on Jan 31, 2007, 11:09 PM)