MYSQL regular expression matching any word in between square brackets - mysql

I have to find all the values of a specific column in a table where the column values match like [name]
I should not get the values that are like [a]+[b] or [a]>[b] or [a]%[b]=[c]
So I basically do not need column values that have special characters in them except the square brackets and under score
example: [test_123] should be returned.
I tried
select * from table_name where column_name REGEXP '^[[][^+-></%]';
This is just trying to see if there is any special character immediately after [ but how to see if there is any special character in the whole column value and should we give backslashes to define special characters in MySQL?
I tried in https://regexr.com/ and I have got my required Regex to be
(\[)\w+[^\+\=\/\*\%\^\!](\])
but I could not do the same in MySQL

*** UPDATED **
(\[[^\_]+])+
That seems to work for what you're looking for.
Also your query is wrong, I believe it is supposed to look like:
SELECT * FROM mytable WHERE REGEXP_LIKE(mycolumn, 'regexp', 'i');

Related

mysql MATCH AGAINST weird characters query

I have a table where the field "company_name" has weird characters, like "à","ö","¬","©","¬","†", etc. I want to return all "company_name"s that contain these characters anywhere within the string. My current query looks like this:
SELECT * FROM table WHERE
MATCH (company_name) AGAINST ('"Ä","à","ö","¬","©","¬","†"' in natural language mode);
But I keep getting no data from the query. I know this can't be the case, as there are definitely examples of them I can find manually. To be clear, the query itself isn't throwing any errors, just not returning any data.
The minimun word length is 3 pr 4 .
you can change it see manial
https://dev.mysql.com/doc/refman/8.0/en/fulltext-fine-tuning.html
or use regular expressiions
SELECT * FROM table WHERE
ompany_name REGEXP '[Äàö¬©¬†]+';
SELECT *
FROM table
WHERE company_name LIKE '%[^0-9a-zA-Z !"#$%&''()*+,\-./:;<=>?#\[\^_`{|}~\]\\]%' ESCAPE '\'
This will find any wacky stuff, including wide characters or 'euro-ASCII' or emoji.

mysql search where column like ''

Quick questions, I bet the answer is so simple and I am just being blind.
I want to select from the database all the names that only start with "test_1_1_".
I would guess that I do this with;
SELECT * FROM my_table WHERE names LIKE "test_1_1_%";
This doesn't seem to work.
The results keep showing up as
test_1_1_1
test_1_1_2
test_1_11_1
test_1_11_2
test_1_12_1
test_1_12_2
How can I select with MySQL only the results that start with "test_1_1_"?
Thank you in advance.
Wesley
Underscore has a special meaning in LIKE and needs to be escaped:
SELECT * FROM my_table WHERE names LIKE 'test\_1\_1\_%';
You could also use REGEXP here, and avoid the escaping problem:
SELECT * FROM my_table WHERE names REGEXP '^test_1_1_';
When you use _ inside LIKE, it will mean replace that space with any character.
For example, searching for something like field LIKE 'a_' will result in any field with 2 characters starting with "a".
If you really want to search for the underscore characters, you need to escape the value with \ and your query will look like this: LIKE 'test\_1\_1\_%';
The underscore is a single character wildcard when use with LIKE. So to specifically locate an underscore it needs to be "escaped" as follows:
select
*
from mytable
where names like 'test\_1\_1\_%' escape '\'
To test for literal instances of a wildcard character, precede it by
the escape character. If you do not specify the ESCAPE character, \ is
assumed.
https://dev.mysql.com/doc/refman/8.0/en/string-comparison-functions.html
Can do this:
SELECT * FROM my_table WHERE names LIKE "test\_1\_\1\_%";

REGEX to Find Rows Containing Opening and Closing Parentheses in MySQL

I have tried:
....WHERE fieldname REGEXP '.*\(.*\).*';
But this returns every record in the table.
You should use double backslashes when escaping a regex special metacharacter in a REGEXP pattern. Also, since REGEXP also finds partial matches, you do not need the .* at the start/end.
So, you could fix the expression as
WHERE fieldname REGEXP '\\(.*\\)';
Or just use LIKE where % matches any amount of arbitrary chars (but must match the whole entry unlike REGEXP):
WHERE fieldname LIKE '%(%)%';
A MySQL fiddle:
DROP TABLE IF EXISTS t;
CREATE TABLE t (word varchar(255));
INSERT INTO t (word)
VALUES
('test (here)'),
('test (here) test'),
('(here) test'),
('test no'),
('no test');
SELECT * FROM t WHERE word REGEXP '\\(.*\\)';
SELECT * FROM t WHERE word LIKE '%(%)%';
To get entries like text ) and ( here, you may use
SELECT * FROM t WHERE word REGEXP '\\(.*\\)|\\).*\\(';
SELECT * FROM t WHERE word LIKE '%(%)%' OR word LIKE '%)%(%';
See another fiddle.
LIKE would be more efficient than REGEXP, but to answer in REGEXP terms...
I suspect the OP wants parentheses to be somewhere in the middle of row text.
The problem with the REGEXP supplied is that expressions are greedy.
As soon as the .* finishes matching, it has already soaked up the entire record.
Try making the first two .* expressions as [^(]( and [^)]) and removing the final .* as it is superfluous.
'[^\\(]*\\([^\\)]*\\)'
Basically, this expression says
Look for zero or more non ( characters
then look for a single ( character
then look for zero or more non ) characters
then look for a single ) character
The () may be anywhere in the record and may contain zero or more characters inside the ().
Might I suggest fiddling in https://regex101.com/
Hope this helps.

SQL - How to use wildcard in LIKE as a normal character

if I have a column with some values that starts with "%" like this:
[ID]-----[VALUES]
1--------Amount
2--------Percentage
3--------%Amount
4--------%Percentage
how can I have only these two rows with a "select" query?:
[ID]-----[VALUES]
3--------%Amount
4--------%Percentage
I tried these queries but them don't work:
select * from TABLE where VALUES like '[%]%'
select * from TABLE where VALUES like '\%%'
I know that in Java, C and other languages, the backspace \ let you use a jolly character as a normal one like:
var s = "I called him and he sad: \"Hi, there!\"";
There is a similar character/function that do it in SQL?
All answers will be appreciated, thank you for reading the question!
Your query
select * from TABLE where VALUES like '\%%'
should work. The reason it doesn't is because you may have NO_BACKSLASH_ESCAPES enabled which would treat \ as a literal character.
A way to avoid it is using LIKE BINARY
select * from TABLE where VALUES like binary '%'
or with an escape character (can be any character you choose) specification.
select * from TABLE where VALUES like '~%%' escape '~'
try this :
select * from TABLE where VALUES like '%[%]%'
There is an ESCAPE option on LIKE:
select *
from TABLE
where VALUES like '$%%' escape '$';
Anything following the escape character is treated as a regular character. However, the default is backslash (see here), so the version with backslash should do what you want.
Of course, you could also use a regular expression (although that has no hope of using an index).
Note: escape is part of the answer standard so it should work in any database.
You're right that you'll need an escape character for this. In SQL you have to define the escape character.
SELECT * FROM TABLE where VALUES like ESCAPE '!';
I'm pretty sure you can use whatever character you want.
Here's a link to a microsoft explanation that goes into more detail.
Microsoft explanation
MySQL Explanation

matching escape charactres using like operator in mysql

I want to match the string having escape characters with particular column in a table.
SELECT * FROM table WHERE col LIKE 'MESSRESTAURANGER AB\\MESSVEGEN 1\\STOCKH';
Though there is matching data in table, query result is empty set. But the same query works fine in oracle. What is the issue with mysql?
You miss %:
SELECT * FROM table WHERE col LIKE '%MESSRESTAURANGER AB\\MESSVEGEN 1\\STOCKH%';
But it should work without escaping:
SELECT * FROM table WHERE col LIKE '%MESSRESTAURANGER AB\MESSVEGEN 1\STOCKH%';
Fiddle http://sqlfiddle.com/#!9/a7ba59/2
EDIT:
SELECT * FROM t WHERE n LIKE '%MESSRESTAURANGER AB\\\\\\\\MESSVEGEN 1\\\\\\\\STOCKH%'
Because MySQL uses C escape syntax in strings (for example, “\n” to
represent a newline character), you must double any “\” that you use
in LIKE strings. For example, to search for “\n”, specify it as “\n”.
To search for “\”, specify it as \\\\; this is because the
backslashes are stripped once by the parser and again when the pattern
match is made, leaving a single backslash to be matched against.
Fiddle http://sqlfiddle.com/#!9/ac46b/9