Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expression Help

Status
Not open for further replies.

uuperl

Programmer
Feb 20, 2006
33
US
I have problem with regular expression on this line:

if ( $systypeopt !~ /[adri]{0,1}/ )

I want valid input like a or a d or a d i or d r i, etc
each option should be appear once in input
if any of the option are out of these four also should be failed and then redo.

Thanks!

#!/usr/bin/perl

%SELOPT_HASH = ( a => "APP", d => "DBS", r => "RDBX", i => "IDBX" );

print "\n";
print "[a] APP\n";
print "[d] DBS\n";
print "[r] RDBX\n";
print " IDBX\n";
print "\n";

{
print "Select System Type: ";
chomp ($systypeopt = <STDIN>);
if ( $systypeopt !~ /[adri]{0,1}/ ) {
print "\t===>Msg: Wrong option in [$systypeopt]. Please try again\n";
redo;
}
}
@systypevalues = split(' ', $systypeopt);
@newsystypevalues="";

foreach my $val (@systypevalues) {
push( @newsystypevalues,$SELOPT_HASH{$val} );
}
print "\t@newsystypevalues\n";
 
Code:
 if ( $systypeopt !~ /^[adri]$/ ) {

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hello Kevin,

if ( $systypeopt !~ /^[adri]$/ ) {

does not work for the following:

[a] APP
[d] DBS
[r] RDBX
IDBX

Select System Type: a d r
===>Msg: Wrong option in [a d r]. Please try again

I want able to enter a d r or a or a i and should say valid.
if I enter a d z then should be failed.

each option enter has space between it.

thanks!
 
also i don't want any of the options repeat more than 1 time, but can be match 0 times.
 
See if this helps:
Code:
my @user_input = ('a d r', 'a d z', 'a d a r', 'a d a z');

foreach my $input (@user_input) {
    my (%inputs, @temp);
    $inputs{$_}++ for split(/\s+/, $input);

    print "$input\t\t";

    if (@temp = grep {/[^ardi]/} sort keys %inputs) {
        print "Invalid Option(s): @temp\n";
        next;
    }

    if (@temp = grep {$inputs{$_} > 1} sort keys %inputs) {
        print "Option(s) used more than once: @temp\n";
        next;
    }

    print "Valid\n";
    # Do DB stuff
}
 
Code:
[gray]#!/usr/bin/perl[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]


[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]%SELOPT_HASH[/blue] = [red]([/red] [purple]a[/purple] => [red]"[/red][purple]APP[/purple][red]"[/red], [purple]d[/purple] => [red]"[/red][purple]DBS[/purple][red]"[/red], [purple]r[/purple] => [red]"[/red][purple]RDBX[/purple][red]"[/red], [purple]i[/purple] => [red]"[/red][purple]IDBX[/purple][red]"[/red] [red])[/red][red];[/red]
[black][b]my[/b][/black] [blue]@newsystypevalues[/blue] = [red]([/red][red])[/red][red];[/red]


[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[black][b]print[/b][/black] [red]"[/red][purple][a]      APP[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[black][b]print[/b][/black] [red]"[/red][purple][d]      DBS[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[black][b]print[/b][/black] [red]"[/red][purple][r]      RDBX[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[black][b]print[/b][/black] [red]"[/red][purple][i]      IDBX[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[black][b]print[/b][/black] [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

[maroon]LOOP[/maroon][maroon]:[/maroon] [red]{[/red]
   [black][b]my[/b][/black] [blue]%seen[/blue] = [red]([/red][red])[/red][red];[/red]
   [black][b]print[/b][/black] [red]"[/red][purple]Select System Type: [/purple][red]"[/red][red];[/red]
   [url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url] [red]([/red][black][b]my[/b][/black] [blue]$systypeopt[/blue] = <STDIN>[red])[/red][red];[/red]
   [black][b]my[/b][/black] [blue]@systypevalues[/blue] = [url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url][red]([/red][red]/[/red][purple][purple][b]\s[/b][/purple]+[/purple][red]/[/red], [blue]$systypeopt[/blue][red])[/red][red];[/red]
   [olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$val[/blue] [red]([/red][blue]@systypevalues[/blue][red])[/red] [red]{[/red]
      [blue]$seen[/blue][red]{[/red][blue]$val[/blue][red]}[/red]++[red];[/red]
      [olive][b]unless[/b][/olive] [red]([/red] [url=http://perldoc.perl.org/functions/exists.html][black][b]exists[/b][/black][/url] [blue]$SELOPT_HASH[/blue][red]{[/red][blue]$val[/blue][red]}[/red] && [blue]$seen[/blue][red]{[/red][blue]$val[/blue][red]}[/red] == [fuchsia]1[/fuchsia] [red])[/red] [red]{[/red]
         [black][b]print[/b][/black] [red]"[/red][purple][purple][b]\t[/b][/purple]===>Msg: Wrong option in [[blue]$systypeopt[/blue]].  Please try again[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
         [olive][b]redo[/b][/olive] LOOP[red];[/red] 
      [red]}[/red]
   [red]}[/red]
[red]}[/red]
[black][b]print[/b][/black] [red]"[/red][purple][purple][b]\t[/b][/purple][blue]@newsystypevalues[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
(Since I just can't help myself but jump into a regular expression discussion...)

perl4fun,

As has already been demonstrated by a few of our more prolific experts, it is a lot easier to solve this type of requirement with logic rather than with a single regular expression. The problem is that your requirements are too complex, specifically the uniqueness check.

It might be possible to do this with a single regex, but it's going to be really convoluted. This is the best compromise that I can come up with.

Code:
[gray]#!/usr/bin/perl[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]%SELOPT_HASH[/blue] = [red]([/red] [purple]a[/purple] => [red]"[/red][purple]APP[/purple][red]"[/red], [purple]d[/purple] => [red]"[/red][purple]DBS[/purple][red]"[/red], [purple]r[/purple] => [red]"[/red][purple]RDBX[/purple][red]"[/red], [purple]i[/purple] => [red]"[/red][purple]IDBX[/purple][red]"[/red] [red])[/red][red];[/red]
[black][b]my[/b][/black] [blue]@newsystypevalues[/blue] = [red]([/red][red])[/red][red];[/red]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[olive][b]foreach[/b][/olive] [red]([/red][url=http://perldoc.perl.org/functions/sort.html][black][b]sort[/b][/black][/url] [url=http://perldoc.perl.org/functions/keys.html][black][b]keys[/b][/black][/url] [blue]%SELOPT_HASH[/blue][red])[/red] [red]{[/red]
	[black][b]print[/b][/black] [red]"[/red][purple][[blue]$_[/blue]]      [blue]$SELOPT_HASH[/blue]{[blue]$_[/blue]}[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]
[black][b]print[/b][/black] [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

[red]{[/red]
    [black][b]print[/b][/black] [red]"[/red][purple]Select System Type: [/purple][red]"[/red][red];[/red]
    [url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red]([/red][black][b]my[/b][/black] [blue]$opt[/blue] = <STDIN>[red])[/red][red];[/red]
    
    [gray][i]# Requirements - Check values, Check uniqueness[/i][/gray]
    [olive][b]if[/b][/olive] [red]([/red] [blue]$opt[/blue] !~ [red]/[/red][purple]^(?:[[blue]@[/blue]{[join '', keys %SELOPT_HASH]}][purple][b]\s[/b][/purple]*)+$[/purple][red]/[/red] || [blue]$opt[/blue] =~ [red]/[/red][purple]([purple][b]\w[/b][/purple]).*[purple][b]\1[/b][/purple][/purple][red]/[/red][red])[/red] [red]{[/red]
        [black][b]print[/b][/black] [red]"[/red][purple][purple][b]\t[/b][/purple]===>Msg: Wrong option in [[blue]$opt[/blue]].  Please try again[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
        [olive][b]redo[/b][/olive][red];[/red]
    [red]}[/red]
    
    [blue]@newsystypevalues[/blue] = [blue]$opt[/blue] =~ [red]/[/red][purple]([purple][b]\w[/b][/purple])[/purple][red]/[/red][red]g[/red][red];[/red]
[red]}[/red]

[black][b]print[/b][/black] [red]"[/red][purple][blue]@newsystypevalues[/blue][/purple][red]"[/red][red];[/red]

However, I would advise you to just use a slightly simplified version of Kevin's code. It's more clear what is going on:

Code:
[gray]#!/usr/bin/perl[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]%SELOPT_HASH[/blue] = [red]([/red] [purple]a[/purple] => [red]"[/red][purple]APP[/purple][red]"[/red], [purple]d[/purple] => [red]"[/red][purple]DBS[/purple][red]"[/red], [purple]r[/purple] => [red]"[/red][purple]RDBX[/purple][red]"[/red], [purple]i[/purple] => [red]"[/red][purple]IDBX[/purple][red]"[/red] [red])[/red][red];[/red]
[black][b]my[/b][/black] [blue]@newsystypevalues[/blue] = [red]([/red][red])[/red][red];[/red]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[olive][b]foreach[/b][/olive] [red]([/red][url=http://perldoc.perl.org/functions/sort.html][black][b]sort[/b][/black][/url] [url=http://perldoc.perl.org/functions/keys.html][black][b]keys[/b][/black][/url] [blue]%SELOPT_HASH[/blue][red])[/red] [red]{[/red]
	[black][b]print[/b][/black] [red]"[/red][purple][[blue]$_[/blue]]      [blue]$SELOPT_HASH[/blue]{[blue]$_[/blue]}[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]
[black][b]print[/b][/black] [red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

[maroon]LOOP[/maroon][maroon]:[/maroon] [red]{[/red]
   [black][b]print[/b][/black] [red]"[/red][purple]Select System Type: [/purple][red]"[/red][red];[/red]
   [url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url] [red]([/red][black][b]my[/b][/black] [blue]$opt[/blue] = <STDIN>[red])[/red][red];[/red]

   [black][b]my[/b][/black] [blue]%seen[/blue] = [red]([/red][red])[/red][red];[/red]
   [olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$val[/blue] [red]([/red][blue]@newsystypevalues[/blue] = [url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url] [red]/[/red][purple][purple][b]\s[/b][/purple]+[/purple][red]/[/red], [blue]$opt[/blue][red])[/red] [red]{[/red]
      [olive][b]if[/b][/olive] [red]([/red]! [url=http://perldoc.perl.org/functions/exists.html][black][b]exists[/b][/black][/url] [blue]$SELOPT_HASH[/blue][red]{[/red][blue]$val[/blue][red]}[/red] || [blue]$seen[/blue][red]{[/red][blue]$val[/blue][red]}[/red]++ [red])[/red] [red]{[/red]
         [black][b]print[/b][/black] [red]"[/red][purple][purple][b]\t[/b][/purple]===>Msg: Wrong option in [[blue]$opt[/blue]].  Please try again[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
         [olive][b]redo[/b][/olive] LOOP[red];[/red]
      [red]}[/red]
   [red]}[/red]
[red]}[/red]

[black][b]print[/b][/black] [red]"[/red][purple][purple][b]\t[/b][/purple][blue]@newsystypevalues[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

- Miller
 
You are all great help on this. Thank you so much for jump in to help me out for this issue!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top