For example, I would like to find an 'exe' with a fixed path: \Temp\3038.exe
The number before '.exe' is fixed to 4 digits, but the value is random(0001 ~ 9999).
Only numbers are changed to 4 random digits.
I'd really appreciate it if you could tell me what to do.
I tried 2ways:
SELECT * FROM myTable WHERE path REGEXP '\\Temp\\\d{4}.exe';
SELECT * FROM myTable WHERE path like '\\Temp\\\d{4}.exe';
my data:
C:\\Users\\AppData\\Local\\Temp\\1536.exe
C:\\Users\\AppData\\Local\\Temp\\6247.exe
C:\\Users\\AppData\\Local\\Temp\\2508.exe
.......(skip)
For these I need to get 1536, 6247, 2508
DBFIDDLE
A regular expression will bite, especially if it has a '` in it.
When you say the data is: C:\\Users\\AppData\\Local\\Temp\\2508.exe will it have one '` or two '' ? to be honest i got a but lost too 😉
The DBFIDDLE uses:
select *
from mytable
where path REGEXP '.*\\\\Temp\\\\\\d{4}.exe';
The REGEXP looks complicated, but let me try to explain, it matches
.* a dot is any character, and an asterix says that character(s) can be present 0 or more times.
\\ a backslash
\\ another backslash
'Temp' the four letters 'T','e','m' and 'p', in this order.
\\ another backslash
\\ another backslash
\d{4} the \d is representing a digit (which is a number between 0 and 9), and the '{4}' will match four of those.
. this is a random character
exe this is the three letters in 'exe'.
A bug in this is that this will also match: C:\\Users\\AppData\\Local\\Temp\\2508Xexe
But, I like to keep room for improvements 😉😁
P.S. When replacing the '.' by '.' it will match a dot literally, and not a random charachter.
EDIT: re-reading the question, I see that you only want the numbers.
From the results you can take LEFT(RIGHT(path,8),4)
This will first take the last 8 charachters (RIGHT(path,8)), and from that the first 4 characters. This should work because the matching regular expression result always end in 4 numbers with '.exe' after it.
Related
I want to check whether there is a known pattern with variable numbers.
This column 'shortcut' has values like this
|shortcuts|
-----------
|ab1
|ab2
|ab23
|abc123
The only thing I've got for my SQL-statement is the alphabetical pattern e.g. 'ab'
So I started with
SELECT * FROM mytable WHERE shortcut LIKE 'ab%'
I only need ab1, ab2 and ab23 and NOT abc12.
Is there a way to modify my statement? There is at least one number, numbers always follow the known pattern and the pattern is the only known value.
You can use regular expressions:
where shortcut regexp '^ab[0-9]+'
This says that shortcut starts with "ab" and is followed by at least one digit.
You can use
SELECT * FROM mytable WHERE shortcut REGEXP '^ab[0-9]+$'
The ^ab[0-9]+$ regex (see its online demo) matches:
^ - start of string
ab - an ab string (case insensitively, use BINARY after REGEXP to make it case sensitive)
[0-9]+ - one or more digits
$ - end of string.
See this regex graph:
I have a table with a lot of individual words in it (Column name 'qWord') with contents including 'Utility', 'Utter', 'Unicorn' and 'Utile'
I'm trying to do a SELECT to find qWord strings which have at most one instance of the letter 't'.
Using REGEXP I thought it would be a trivial statement like:
SELECT *
FROM entries.qentries
WHERE (qWord REGEXP 'T{0,1}')
but I'm still getting 'Utter' and 'Utility' in the output -- along with 'Utile' and 'Unicorn'.
So what am I missing here?
(FWIW: MySQL 8.0.11, Community edition running on a Windows 8.1 machine)
Here's the full REGEXP and my apologies for not posting it initially. I'm looking for words composed only of specific letters and that part works fine.
But I also words with a limited number of a given letter, say t
SELECT * FROM entries.entries WHERE
(qWord NOT REGEXP 'C|F|G|I|J|K|P|Q|S|V|W|X|Y|Z|-')
AND (qWord REGEXP 'A|B|D|E|H|L|M|N|O|R|T|U')
AND (qWord REGEXP 't{0,1}') ;
I've also tried (qWord REGEXP 't{0}|t{1}') as well as (qWord REGEXP '(?<=[^t]|^)(t{0}|t{1})(?:[^t]|$)' )
without success, so I remain stuck
You can use the following regex:
SELECT *
FROM entries.qentries
WHERE (qWord REGEXP '^[^tT]*[tT]?[^tT]*$')
Explanations:
^, $ starting and ending anchors (this is needed to avoid word partial match)
[^tT]* any character that is not a t or a T 0 or more times
[tT]? at most one occurrence of t or T (? is equivalent to {0,1})
[^tT]* any character that is not a t or a T 0 or more times
Regex Demo
Additional Notes:
[^tT] this character range will accept anything that is not a t or a T (spaces, ., \n and other characters will also be accepted, you can restrict this if you want to accept only letters and exclude the t,T you can use: [a-su-zA-SU-Z], if you want to add other characters to this class, just add them at the end [a-su-zA-SU-Z -] will also accept words with spaces and -.
I have a list of numbers in some fields in a table, for example something like this:
2033,1869,1914,1913,19120,1911,1910,1909,1908,1907,1866,1921,1922,1923
Now, I'm trying to do a query to check if a number is found in the row, however, I can't use LIKE as then it may return false positives as if I did a search for 1912 in the above field I would get a result returned because of the number 19120, obviously we don't want that - we can't append or prepend a comma as the start/end numbers don't have them.
So, onto using REGEXP I go... I tried this, but it doesn't work (it returns a result):
SELECT * FROM cat_listing WHERE cats REGEXP '[^0-9]*1912[^0-9]*';
I imagine why it still finds something is because of the * quantifier; it found [^0-9] 0 times AFTER 1912 so it considers it a match.
I'm not sure how to modify it to do what I want.
In your case, it seems word boundaries are necessary:
SELECT * FROM cat_listing WHERE cats REGEXP '[[:<:]]1912[[:>:]]';
[[:<:]] is the beginning of a word and [[:>:]] is the end. See reference:
[[:<:]], [[:>:]]
These markers stand for word boundaries. They match the beginning and end of >words, respectively. A word is a sequence of word characters that is not >preceded by or followed by word characters. A word character is an alphanumeric >character in the alnum class or an underscore (_).
You have another option called find_in_set()
SELECT * FROM cat_listing WHERE find_in_set('1912', cats) <> 0;
Returns 0 if str is not in strlist or if strlist is the empty string. Returns NULL if either argument is NULL. This function does not work properly if the first argument contains a comma (“,”) character.
No need to use a regex just because the column value has no comma at either end:
SELECT
cats
FROM cat_listing
WHERE INSTR(CONCAT(',', cats, ','), ',1912,')
;
See it in action: SQL Fiddle.
Please comment if adjustment / further detail is required.
I would like to find all records from 'mytable' which in the field 'name' used only characters listed below:
Ø-*³`!/-;$€"“- „”\ø,Ø:’.#*+_/? !œ³¥Φ?+#=–()<>ąĄćĆęĘłŁńŃóÓśŚż ŻźŹàáâåéÉêéíıñçãėÊèÈçßœŒæğîïİşúūýōòÒô
regular letters from a to z (and A to Z)
number 0,1,3,4,5,6,7,8,9
spaces and some 'tab' signs
This query does not work:
SELECT name
FROM mytable
WHERE name not regexp '[^a-zA-Z0-9Ø-*³`!/-;$€"“- „”\ø,Ø:’.#*+_/? !œ³¥Φ?+#=–()<>ąĄćĆęĘłŁńŃóÓśŚż ŻźŹàáâåéÉêéíıñçãėÊèÈçßœŒæğîïİşúūýōòÒô]'
I know that this solution is far from good :) but I've tried different methods - this one returns the result closest to the required. Can you please give me some hint?
You can take advantage of character classes.
For example, instead of [ąĄóÓōòÒô...] use [[=A-Za-z=]].
This will match any letters from a through z igoring case and ignoring if the letter has accent.
Check the documentation for additional characters classes that will match your missing characters.
I'm using a stored procedure to validate the input parameter. The input parameter must contain a-z and A-Z and 0-9.
for Example:
aS78fhE0 -> Correct
76AfbRZt -> Correct
76afbrzt -> Incorrect(doesn't contain Upper Case A-Z)
asAfbRZt -> Incorrect(doesn't contain Numeric 0-9)
4QA53RZJ -> Incorrect(doesn't contain Lower Case a-z)
what Regular Expression that can validate the input parameter like above example,.?
Many Thanks,Praditha
UPDATEOthers character except Alphanumeric are not allowedI'm Using MySQL version 5
Further from Johns Post and subsequent comments:
The MySql you require would be
SELECT * FROM mytable WHERE mycolumn REGEXP BINARY '[a-z]'
AND mycolumn REGEXP BINARY '[A-Z]'
AND mycolumn REGEXP BINARY '[0-9]'
Add additional
AND mycolum REGEXP BINARY '^[a-zA-Z0-9]+$'
If you only want Alphanumerics in the string
With look-ahead assertion you could do like this:
/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$/
update: It seems mysql doesn't support look around assertions.
You could split it up into 3 separate regex to test for each case.
[a-z], [A-Z], and [0-9]
and the results of those matches together, and you can achieve the result you're looking for.
EDIT:
if you're only looking to match alphanumerics, you should do ^[a-zA-Z0-9]+$ as suggested by Ed Head in the comments
My solution is leads to a long expression becuase i will permutate over all 6 possibilities the found capital letter, small letter and the needed number can be arranged in the string:
^(.*[a-z].*[A-Z].*[0-9].*|
.*[a-z].*[0-9].*[A-Z].*|
.*[A-Z].*[a-z].*[0-9].*|
.*[A-Z].*[0-9].*[a-z].*|
.*[0-9].*[a-z].*[A-Z].*|
.*[0-9].*[A-Z].*[a-z].*)$
Edit: Forgot the .* at the end and at the beginning.
Unfortunately, MySQL does not support lookaround assertions, therefore you'll have to spell it out for the regex engine (assuming that only those characters are legal):
^(
[A-Za-z0-9]*[a-z][A-Za-z0-9]*[A-Z][A-Za-z0-9]*[0-9][A-Za-z0-9]*|
[A-Za-z0-9]*[a-z][A-Za-z0-9]*[0-9][A-Za-z0-9]*[A-Z][A-Za-z0-9]*|
[A-Za-z0-9]*[A-Z][A-Za-z0-9]*[a-z][A-Za-z0-9]*[0-9][A-Za-z0-9]*|
[A-Za-z0-9]*[A-Z][A-Za-z0-9]*[0-9][A-Za-z0-9]*[a-z][A-Za-z0-9]*|
[A-Za-z0-9]*[0-9][A-Za-z0-9]*[a-z][A-Za-z0-9]*[A-Z][A-Za-z0-9]*|
[A-Za-z0-9]*[0-9][A-Za-z0-9]*[A-Z][A-Za-z0-9]*[a-z][A-Za-z0-9]*
)$
or, in MySQL:
SELECT * FROM mytable WHERE mycolumn REGEXP BINARY "^([A-Za-z0-9]*[a-z][A-Za-z0-9]*[A-Z][A-Za-z0-9]*[0-9][A-Za-z0-9]*|[A-Za-z0-9]*[a-z][A-Za-z0-9]*[0-9][A-Za-z0-9]*[A-Z][A-Za-z0-9]*|[A-Za-z0-9]*[A-Z][A-Za-z0-9]*[a-z][A-Za-z0-9]*[0-9][A-Za-z0-9]*|[A-Za-z0-9]*[A-Z][A-Za-z0-9]*[0-9][A-Za-z0-9]*[a-z][A-Za-z0-9]*|[A-Za-z0-9]*[0-9][A-Za-z0-9]*[a-z][A-Za-z0-9]*[A-Z][A-Za-z0-9]*|[A-Za-z0-9]*[0-9][A-Za-z0-9]*[A-Z][A-Za-z0-9]*[a-z][A-Za-z0-9]*)$";
[a-zA-Z0-9]*[a-z]+[a-zA-Z0-9]*[A-Z]+[a-zA-Z0-9]*[0-9]+[a-zA-Z0-9]*|[a-zA-Z0-9]*[a-z]+[a-zA-Z0-9]*[0-9]+[a-zA-Z0-9]*[A-Z]+[a-zA-Z0-9]*|[a-zA-Z0-9]*[A-Z]+[a-zA-Z0-9]*[a-z]+[a-zA-Z0-9]*[0-9]+[a-zA-Z0-9]*|[a-zA-Z0-9]*[A-Z]+[a-zA-Z0-9]*[0-9]+[a-zA-Z0-9]*[a-z]+[a-zA-Z0-9]*|[a-zA-Z0-9]*[0-9]+[a-zA-Z0-9]*[A-Z]+[a-zA-Z0-9]*[a-z]+[a-zA-Z0-9]*|[a-zA-Z0-9]*[0-9]+[a-zA-Z0-9]*[a-z]+[a-zA-Z0-9]*[A-Z]+[a-zA-Z0-9]*