Reputation: 37
Alright, I've had a some nightmares with it already so i bow my stupid head to the almighty hive-mind of the Stackoverflow.
UPD: the parameters of my task have been changed. I need to find the codes that could include special chars like parentheses (), and/or @, #, &, $.
So the codes could look like:
"A", "Bb", "2C8", "A7(BO)19", B29H$, 29H(6JI)0# etc
The problem is that all these codes are optional. I've tried like Barmar (see reply 1) suggested, but slightly modifing the MySQL query:
SELECT *
FROM table
WHERE column REGEXP '^[a-z0-9\@\#\$\&()]*$'
AND column LIKE CONCAT('%', '$v', '%')
It cannot return me "S(LJ)9" or "09S(LJ)3$" if i seek for "SLJ" or "S(LJ)"
Well, aside some real important nucleotide sequence in my DNA that would allow me to use brains more efficiently (or have them), what am i missing in this regex or the query itself? Thanks anyone in advance.
Upvotes: 3
Views: 1082
Reputation: 780879
SELECT *
FROM table
WHERE column REGEXP '^[a-z0-9()]*$' /* code contains alphanumerics and parentheses */
AND column NOT REGEXP '[0-9]{3}' /* code does not contain >2 digits in a row */
AND column LIKE CONCAT('%', ?, '%') /* code contains the user input */
where ? is bound with the user input (you should be using PDO or mysqli prepared statements).
Upvotes: 2