Forum Moderators: coopster
I'm trying to match a string which contains a "+" sign, but it won't have it, even if I escape the plus sign:-
mysql> select ("+12312345" REGEXP "\+12");ERROR 1139 (42000): Got error 'repetition-operator operand invalid' from regexp
I basically need to match anything which contains "+12".
Thanks!
TJ
select ("+12312345" REGEXP "\\\+12");
I hope this is correct; I looked at it for two seconds hehe
mysql> select ("+12312345" REGEXP "\+12"); \> \> "\+" => <\+> "\+" => <+> "\+" => Parse Errorselect ("+12312345" REGEXP "\+12"); select ("+12312345" REGEXP "\\\+12"); select ("+12312345" REGEXP "\\+12");First, the string itself is read, and any applicable string-escapes occur: select (<+12312345> REGEXP <+12>); select (<+12312345> REGEXP <\+12>); select (<+12312345> REGEXP <\+12>);Then it parses the regular expression: select (<+12312345> REGEXP /\+12/); select (<+12312345> REGEXP /\+12/);This knowledge becomes even more important when you're looking at running the query through, say, PHP and thus need yet another level of string-escapes:
mysql_query('select ("+12312345" REGEXP "\\\\+12");'); mysql_query(<select ("+12312345" REGEXP "\\+12");>); mysql_query(<select (<+12312345> REGEXP <\+12>);>); mysql_query(<select (<+12312345> REGEXP /\+12/);>); Another (probably better and less confusing) way to look at it is to start at the regular expression and work your way back to the string, adding escapes each time:
[edited by: eelixduppy at 6:55 pm (utc) on April 18, 2007]
[edit reason] disabled smile faces [/edit]
I'll give you an explaination as to why your original doesn't work, and then you can experiment with two or three slashes ;)
The reason why the original wasn't working is because the mysql parser interprets one of the slashes, and then the regex has nothing to interpret. So you see, mysql parses it before the regex, so the slash is interpretted by the wrong thing. To get around this I know of two ways. The solution that I posted above , which I originally wanted to post this:
select ("+12312345" REGEXP "\\+12");
should work, but also something like this should work:
select ("+12312345" REGEXP "[+]12");
And this is because the character class doesn't need the plus-sign to be escaped.
I've confused myself now about using two or three slashes but I believe it is two. If the three-slash solution in my first response works for you, then...well....it works ;)
Nice post, pinterface, thanks :)
I believe it is briefly mentioned in the documentation [dev.mysql.com], too.
And the MySQL manual page linked from that old thread:
[dev.mysql.com...]