=Brad Ummer wrote:
> I need to be able to try and match a short string inputted on a web form against a
>longer string that I'm pulling from a database. Normally this would be easy, but the
>text string that the web user might enter could contain a parenthesis, and that seems
>to be throwing the match off. A simple example:
>
> $web_string = "word(1)";
> $db_string = "word(1),word(2),word(3)";
>
> $_ = $db_string;
>
> if (m/$web_string/) {print "match"}
> else {print "no match"};
>
> The result of this is "no match" since the parentheses are being evaluated somehow.
>If I remove one of the parentheses in one of the strings, I get an error "unmatched
>() in regexp". If I remove all of the parentheses from both strings, I get "match".
>Is there a way that I can get grep to consider everything in the strings, including
>the parentheses, as simple characters? If I can't, do I need to clean every string
>before the match, removing parentheses and any other offending characters? Thanks in
>advance.
## Do this:
=cut
###############################
#!perl -l
$db_string = "word(1),word(2),word(3)";
$_ = 'word(1)'; ## this is what you want to be found
for $disable_parens(0,1) {
if ($disable_parens) {
s,([()]),\\$1,g } ## won't work without this line "(L)"
## or instead of the previous line the following 2 lines:
## s,\(,\\(,g;
## s,\),\\),g;
if ($db_string =~ m,$_,) {
print "$_ matches $db_string." }
else {
print "$_ does not match $db_string." } }
###############################
## The reason, why your search does not work without line "(L)"
## is clear; in
## m,abc,
## Perl will interpolate abc; thus
## (1) means: search for 1 and put it (by virtue of those
## parentheses) into the value $1 ; you can test it if you take away
## the first pair of parens and print $1.
## If you want (1) to be understood verbose, you have to
## disable those parentheses by means of backslashes; this is done in
## line "(L)".
## HTH,
## Detlef