For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > February 2006 > Matching a string containing '+'









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Matching a string containing '+'
Sara

2006-02-08, 8:53 am

Using this query in my Perl Script to find the Category title from the
database given as:
C_and_C++/Forums
C_and_C++/Codes
C_and_C++/Sites
etc.
my $sql = qq(SELECT CAT_TITLE, FROM categories WHERE CAT_TITLE REGEXP
'^C_and_C\\+\\+/[a-zA-Z|_]+\$' )

$query = $dbh -> prepare ($sql) or die "Couldn't prepare statement: " .
$dbh->errstr;

$query->execute();

And when the query is executed, I am gettin' error:
"DBD::mysql::st execute failed: Got error 'repetition-operator operand
invalid' from regexp"
I am using double \\ in front of + as advised on the link given below but
still gettin' error, any solution to it?

http://213.136.52.42/bug.php?id=399

Charles K. Clarkson

2006-02-08, 8:53 am

Sara wrote:

[snip]
: my $sql = qq(
: SELECT CAT_TITLE,
: FROM categories
: WHERE CAT_TITLE REGEXP '^C_and_C\\+\\+/[a-zA-Z|_]+\$'
: );
[snip]

Why is there an escape character in front of the $ anchor?
There isn't one in any of the examples in the linked content.
Try this. It avoids the annoying escapes with character classes
and better highlights that stray escape ''.

'^C_and_C[+][+]/[a-zA-Z|_]+$'


Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328


Paul Lalli

2006-02-08, 8:53 am

Sara wrote:
> Using this query in my Perl Script to find the Category title from the
> database given as:
> C_and_C++/Forums
> C_and_C++/Codes
> C_and_C++/Sites
> etc.
> my $sql = qq(SELECT CAT_TITLE, FROM categories WHERE CAT_TITLE REGEXP
> '^C_and_C\\+\\+/[a-zA-Z|_]+\$' )
>
> $query = $dbh -> prepare ($sql) or die "Couldn't prepare statement: " .
> $dbh->errstr;
>
> $query->execute();
>
> And when the query is executed, I am gettin' error:
> "DBD::mysql::st execute failed: Got error 'repetition-operator operand
> invalid' from regexp"
> I am using double \\ in front of + as advised on the link given below but
> still gettin' error, any solution to it?
>
> http://213.136.52.42/bug.php?id=399


The above link is talking about sending the query directly to the
database, and that the database itself requires two backslashes. You
only passed one backslash, because in a Perl string, \ is a special
character, and \\ is the way you create a literal \.

Annoyingly enough, it would appear your Perl string will need four
backslashes in order to get everything working correctly.

Debugging tip: Always print out the SQL you think you're sending to the
database, to verify it contains what you think it contains. Then take
that SQL and send it directly to the database, in this case via the
mysql command line program (or a program like phpMyAdmin, if you have
it). If you're still getting the same error, you have a Database
problem, not a Perl problem.

Paul Lalli

Paul Lalli

2006-02-08, 8:53 am


Charles K. Clarkson wrote:
> Sara wrote:
>
> [snip]
> : my $sql = qq(
> : SELECT CAT_TITLE,
> : FROM categories
> : WHERE CAT_TITLE REGEXP '^C_and_C\\+\\+/[a-zA-Z|_]+\$'
> : );
> [snip]
>
> Why is there an escape character in front of the $ anchor?


Because the OP has the entire string in double quotes, so Perl would be
looking for the $' variable without that backslash

> There isn't one in any of the examples in the linked content.


None of the linked content was dealing with Perl.

> Try this. It avoids the annoying escapes with character classes
> '^C_and_C[+][+]/[a-zA-Z|_]+$'


Ew. Extra work for the parser, extra work for the reader.

> and better highlights that stray escape ''.


There was no stray escape.

Paul Lalli

Herschel

2006-02-08, 8:53 am

Sara wrote:

> Using this query in my Perl Script to find the Category title from the
> database given as:
> C_and_C++/Forums
> C_and_C++/Codes
> C_and_C++/Sites
> etc.
> my $sql = qq(SELECT CAT_TITLE, FROM categories WHERE CAT_TITLE REGEXP
> '^C_and_C\\+\\+/[a-zA-Z|_]+\$' )
>
> $query = $dbh -> prepare ($sql) or die "Couldn't prepare statement: "
> . $dbh->errstr;
>
> $query->execute();
>
> And when the query is executed, I am gettin' error:
> "DBD::mysql::st execute failed: Got error 'repetition-operator operand
> invalid' from regexp"
> I am using double \\ in front of + as advised on the link given below
> but still gettin' error, any solution to it?
>
> http://213.136.52.42/bug.php?id=399
>

See Clarkson's response on the grep. What bothers me is the WHERE
clause, which seems to be lacking an equivalence or other matching
condition. That is it equal to or LIKE (where the latter requires a
wild card character is the databases I have used).
Paul Lalli

2006-02-08, 6:55 pm

Herschel wrote:
> Sara wrote:
[color=darkred]
> What bothers me is the WHERE
> clause, which seems to be lacking an equivalence or other matching
> condition.


No, it's not.

> That is it equal to or LIKE (where the latter requires a
> wild card character is the databases I have used).


You seem to be missing a great number of words in that sentence.

Regardless, the REGEXP comparison function is well documented.

http://dev.mysql.com/doc/refman/4.1...-functions.html

Paul Lalli

Greg Jetter

2006-02-08, 9:55 pm

On Tuesday February 7 2006 9:52 pm, Sara wrote:
> Using this query in my Perl Script to find the Category title from the
> database given as:
> C_and_C++/Forums
> C_and_C++/Codes
> C_and_C++/Sites
> etc.
> my $sql = qq(SELECT CAT_TITLE, FROM categories WHERE CAT_TITLE REGEXP
> '^C_and_C\\+\\+/[a-zA-Z|_]+\$' )
>
> $query = $dbh -> prepare ($sql) or die "Couldn't prepare statement: " .
> $dbh->errstr;
>
> $query->execute();
>
> And when the query is executed, I am gettin' error:
> "DBD::mysql::st execute failed: Got error 'repetition-operator operand
> invalid' from regexp"
> I am using double \\ in front of + as advised on the link given below but
> still gettin' error, any solution to it?
>
> http://213.136.52.42/bug.php?id=399


Hi Sara , I found when using MySql and perl together and attempting to phrase
RegExp , I have had to double the number of escape chars , thus \\ becomes
\\\\ , then it works , thers a explanation for this behavior in the mysql
refrence manual ...somthing about the mysql engin stripping out escape
chars ... I Just know that it works for me when i double the escapes .

hope it helps you out ..

Greg Jetter
Alaska Internet Solutions

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com