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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting a string to a regular expression 1

Status
Not open for further replies.

ralisp

MIS
Apr 19, 2004
21
0
0
US
Not a Perl Expert. I'm trying to take a string and convert to a regular expression: for example: svr123
convert to /(s|S)(v|V)(R|r)123/
I would need to convert the string into an array and create a new string with the upper and lower case character to create a regular expression to use in another script.
 
I need to create a regular expression that I can pass to another program (outside of the perl script ) using the format listed above. I need a regex is case insensitive, so I would match svr123, Svr123, SVR123, sVr123,etc. So it needs to read in the string and create a new string: /(s|S)(v|V)(R|r)123/
 
Hi

If the regular expression will not be interpreted by [tt]perl[/tt], then for [tt]perl[/tt] it is just a string. So it is up to the unspecified "another program" to handle it and is none of [tt]perl[/tt]'s business as far as I understand.

If you want the pattern and the case insensitive modifier in a single string, take a look at thread219-1569442.

Feherke.
 
Figured it out, this is what I ended up with. Seems to work:


@svr = split '', $server;
my $arrlen = @svr;
@filter="/";
my $lower;
my $upper;
#my $upper=uc($server);
for ( $i = 0; $i < $arrlen; $i++) {
if ( @svr[$i] == /[0-9]/ ) {
push @filter,'[';
$upper=uc(@svr[$i]);
push @filter,$upper;
$lower=lc(@svr[$i]);
push @filter,$lower;
push @filter,']';
}
else {
push @filter, @svr[$i];
}


}
push @filter, "/";
print @filter;
 
Hi

Make yourself a favor, turn warnings on. That way you will find out that
Code:
Scalar value @svr[$i] better written as $svr[$i] at ralisp.pl line 8.
Scalar value @svr[$i] better written as $svr[$i] at ralisp.pl line 10.
Scalar value @svr[$i] better written as $svr[$i] at ralisp.pl line 12.
Scalar value @svr[$i] better written as $svr[$i] at ralisp.pl line 17.
Use of uninitialized value $_ in pattern match (m//) at ralisp.pl line 8.
Argument "x" isn't numeric in numeric eq (==) at ralisp.pl line 8.
So applying the corrections your code should look like this :
Code:
[navy]@svr[/navy] [teal]=[/teal] [b]split[/b] [green][i]''[/i][/green][teal],[/teal] [navy]$server[/navy][teal];[/teal]
[b]my[/b] [navy]$arrlen[/navy] [teal]=[/teal] [navy]@svr[/navy][teal];[/teal]
[navy]@filter[/navy][teal]=[/teal][green][i]"/"[/i][/green][teal];[/teal]
[b]my[/b] [navy]$lower[/navy][teal];[/teal]
[b]my[/b] [navy]$upper[/navy][teal];[/teal]
[gray]#my $upper=uc($server);[/gray]
[b]for[/b] [teal]([/teal] [navy]$i[/navy] [teal]=[/teal] [purple]0[/purple][teal];[/teal] [navy]$i[/navy] [teal]<[/teal] [navy]$arrlen[/navy][teal];[/teal] [navy]$i[/navy][teal]++)[/teal] [teal]{[/teal]
    [b]if[/b] [teal]([/teal] [navy][highlight]$[/highlight]svr[/navy][teal][[/teal][navy]$i[/navy][teal]][/teal] [teal][highlight]!~[/highlight][/teal] [green][i]/[0-9]/[/i][/green] [teal])[/teal] [teal]{[/teal]
        [b]push[/b] [navy]@filter[/navy][teal],[/teal][green][i]'['[/i][/green][teal];[/teal]
        [navy]$upper[/navy][teal]=[/teal][b]uc[/b][teal]([/teal][navy][highlight]$[/highlight]svr[/navy][teal][[/teal][navy]$i[/navy][teal]]);[/teal]
        [b]push[/b] [navy]@filter[/navy][teal],[/teal][navy]$upper[/navy][teal];[/teal]
        [navy]$lower[/navy][teal]=[/teal][b]lc[/b][teal]([/teal][navy][highlight]$[/highlight]svr[/navy][teal][[/teal][navy]$i[/navy][teal]]);[/teal]
        [b]push[/b] [navy]@filter[/navy][teal],[/teal][navy]$lower[/navy][teal];[/teal]
        [b]push[/b] [navy]@filter[/navy][teal],[/teal][green][i]']'[/i][/green][teal];[/teal]
    [teal]}[/teal]
    [b]else[/b] [teal]{[/teal]
        [b]push[/b] [navy]@filter[/navy][teal],[/teal] [navy][highlight]$[/highlight]svr[/navy][teal][[/teal][navy]$i[/navy][teal]];[/teal]
    [teal]}[/teal]
    
    
[teal]}[/teal]
[b]push[/b] [navy]@filter[/navy][teal],[/teal] [green][i]"/"[/i][/green][teal];[/teal]
[b]print[/b] [navy]@filter[/navy][teal];[/teal]

Feherke.
 
Thank you for letting me know, I'll make the changes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top