Syntax checking of user input

M

mike

hi

i need to get user input and filter the input like this :
pass(func1) (OR|AND) pass(anyinput) (OR|AND) pass(func)

the conditions to be met are
1) check for any number of the word "pass"
2) only OR and AND can be used. Therefore the following are valid:
- pass(anywords) AND pass(anywords2) OR pass(anyany)
- pass(anywords) AND pass(anywords3)
- pass(anywords)
3) any number of words and digits can be inside the parenthesis
eg pass(abcd) or pass(1d2ad) etc
4) cannot have a "runaway" OR or AND, like this
- pass(asdfa) OR

an initial attempt is

my $input = <STDIN>;
chomp($input);
if ( $input =~ /(OR|AND)$/ )
{ failed() ; }
elsif ( $input =~ /^pass\((.*)\)$/ )
{ dosomething(); }



but i am stuck with this input condition whereby user keys in many
"pass" ,
something like this:
pass(args1) OR pass(args2) AND pass(args3) OR pass(args4) AND
pass(args5) OR pass(1234) AND pass(args6) OR pass(aafs)

in a situation like this, how can i perform input validation using
regular expressions in perl?

thanks
 
G

Gregory Toomey

It was a dark and stormy night, and mike managed to scribble:
hi

i need to get user input and filter the input like this :
pass(func1) (OR|AND) pass(anyinput) (OR|AND) pass(func)

the conditions to be met are
1) check for any number of the word "pass"
2) only OR and AND can be used. Therefore the following are valid:
- pass(anywords) AND pass(anywords2) OR pass(anyany)
- pass(anywords) AND pass(anywords3)
- pass(anywords)
3) any number of words and digits can be inside the parenthesis
eg pass(abcd) or pass(1d2ad) etc
4) cannot have a "runaway" OR or AND, like this
- pass(asdfa) OR

an initial attempt is

my $input = <STDIN>;
chomp($input);
if ( $input =~ /(OR|AND)$/ )
{ failed() ; }
elsif ( $input =~ /^pass\((.*)\)$/ )
{ dosomething(); }



but i am stuck with this input condition whereby user keys in many
"pass" ,
something like this:
pass(args1) OR pass(args2) AND pass(args3) OR pass(args4) AND
pass(args5) OR pass(1234) AND pass(args6) OR pass(aafs)

in a situation like this, how can i perform input validation using
regular expressions in perl?

thanks

You really want a parser - check out Parse::RecDescent in www.cpan.org .
You need to consider the operator precidence of AND and OR.

gtoomey
 
B

Bob Walton

mike wrote:

....
i need to get user input and filter the input like this :
pass(func1) (OR|AND) pass(anyinput) (OR|AND) pass(func)

the conditions to be met are
1) check for any number of the word "pass"
2) only OR and AND can be used. Therefore the following are valid:
- pass(anywords) AND pass(anywords2) OR pass(anyany)
- pass(anywords) AND pass(anywords3)
- pass(anywords)
3) any number of words and digits can be inside the parenthesis
eg pass(abcd) or pass(1d2ad) etc
4) cannot have a "runaway" OR or AND, like this
- pass(asdfa) OR

an initial attempt is

my $input = <STDIN>;
chomp($input);
if ( $input =~ /(OR|AND)$/ )
{ failed() ; }
elsif ( $input =~ /^pass\((.*)\)$/ )
{ dosomething(); }



but i am stuck with this input condition whereby user keys in many
"pass" ,
something like this:
pass(args1) OR pass(args2) AND pass(args3) OR pass(args4) AND
pass(args5) OR pass(1234) AND pass(args6) OR pass(aafs)

in a situation like this, how can i perform input validation using
regular expressions in perl?


A situation like exactly what? Is that intended to be a single input
line which was wrapped somewhere between your posting and my viewing, or
is it supposed to represent a way of continuing an expression from one
line to another? If the former, then a regex like:

/^pass\([^)]*?\)(?:\s+(?:AND|OR)\s+pass\([^)]*?\))*$/

should work to tell you if you have a valid expression according to your
stated rules or not. That's assuming stuff with parens can't be inside
the pass() calls, etc.

Here is a complete example:

use strict;
use warnings;
while(<DATA>){
chomp;
print "input: $_\n";
if(/^pass\([^)]*?\)(?:\s+(?:AND|OR)\s+pass\([^)]*?\))*$/){
print "valid\n";
}
else{
print "invalid\n";
}
}
__END__
pass(anywords) AND pass(anywords2) OR pass(anyany)
pass(anywords) AND pass(anywords3)
pass(anywords)
[next line wrapped when posted]
pass(args1) OR pass(args2) AND pass(args3) OR pass(args4) AND
pass(args5) OR pass(1234) AND pass(args6) OR pass(aafs)
pass(sdflkj) AND pass(sdflkj) OR

....
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top