Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Regular expression question

by TASdvlper (Monk)
on Apr 26, 2004 at 16:28 UTC ( [id://348235]=perlquestion: print w/replies, xml ) Need Help??

TASdvlper has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,

Thanks to a lot of help here, I'm getting better at regexp, but I have a problem where I'm not even sure where to start.

I want to check a string that contains one or more of the following comma seperated values (in any order): -config,-files,-params,-driverPath,-appPath,-batchPath. I do not want to allow duplicates or the string could match: NONE. So, following are some examples:

$s = "-config,-files" VALID $s = "-files,-batchPath" VALID $s = "-files,-batch-driverPath" NOT VALID, not comma seperated. $s = "NONE" VALID $s = "NONE,-params" NOT VALID, can not mix [NONE] with other values +. $s = " -config, -files " NOT VALID, spaces not allowed (beginning, m +iddle or end) $s = "-test" NOT VALID, not a valid parameter $s = "-files,-test,-batchPath" NOT VALID, valid parameters mixed wit +h invalid parameters. $s = "-files,files" NOT VALID, same valid listed 2x $s = "-config,-files,-params,-driverPath,-appPath,-batchPath" VALI +D
Any help would be greatly appreciated.

Replies are listed 'Best First'.
Re: Regular expression question
by kvale (Monsignor) on Apr 26, 2004 at 16:41 UTC
    Frankly, I would not use a regexp for this (untested) :
    my %opts; undef @opts{ qw|-config -files -params -driverPath -appPath -batchPath NONE| }; my %seen; for my $e (split /,/ $str) { print "BAD op: $e\n" unless exists $opts{$e}; print "Duplicate: $e\n" if $seen{$e}; print "Incompatible with NONE\n" if $seen{NONE} && scalar keys %see +n > 1; $seen{$e}; }

    -Mark

Re: Regular expression question
by japhy (Canon) on Apr 26, 2004 at 17:14 UTC
    Assuming you have the string in $s:
    my %on = (); $s .= ","; unless ($s =~ s/NONE,//) { $on{$_} = $s =~ s/-$_,// for qw( config files params driverPath appPath batchPath ); } die if length $s;
    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;
Re: Regular expression question
by ccn (Vicar) on Apr 26, 2004 at 16:42 UTC
    sub is_allowed { my $s = shift; return 1 if $s eq 'NONE'; my %allowed, %seen; @allowed{split /,/,'-config,-files,-params,-driverPath,-appPath,-b +atchPath'} = (); ### (incorrect line)return if !exists $allowed{$_} or $seen{$_}++ +foreach split /,/, $s; (!exists $allowed{$_} or $seen{$_}++) and return foreach split /, +/, $s; return 1; }
    Upd: incorect "EXPR if COND for LIST" construction fixed
      return if !exists $allowed{$_} or $seen{$_}++ foreach split /,/, $ +s;
      That won't work as you can't have stacked statement modifiers.
Re: Regular expression question
by CountZero (Bishop) on Apr 26, 2004 at 19:25 UTC
    Instead of splitting on comma, I'd split on ,-

    Then run the results of the split against the array or list of acceptable keywords (less the starting - of course).

    Doing so would automatically invalidate the following cases:

    • -files,-batch-driverPath : "batch-driverPath" will not be recognized as a valid keyword
    •  -config, -files : "-config, -files " will not be split on ,- so it will never form a valid keyword

    As this split keeps leading and trailing whitespace, the leading and trailing whitespace cases are invalidated as well.

    The only things you still have to do "by hand" is checking for incompatible combinations or multiple occurrences of the same keywords. This is left as an exercise for the readers.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://348235]
Approved by Limbic~Region
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-24 06:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found