MySQL REGEXP matching positive numbers - mysql

I have a column varchar[25] with this data inside :
886,-886
-886
0,-1234
1234
(empty)
0
the numbers might change in size from a 1 digit to a n digits.
and I need to be able to pull any row that has at least one positive number in it
I was thinking that something like
REGEXP '[^-,][0-9]+'
but this pulls -886 as 88 matches the regexp

you probably does not require regex
COL not like '-%' AND COL not like '%,-%'
however, this is the bad example of storing into incorrect data type,
split , and store into multiple rows ...and you can save some time for handling something like this question

Try using this :
"[^-\d]\d+\b"
which should work if i understood your question correctly.
a good regex reference table : http://www.regular-expressions.info/reference.html

I was able to figure out the best solution:
`COL` NOT LIKE '%-%'
I forgot to mention that the column might also contain words like:
all,-886
none,886
0,1,2,3,none
0
etc...

Try
REGEXP '[[:<:]][^-,][0-9]+[[:>:]]'
The :<: and :>: portions indicate word boundaries.

^\b\d+\b$
will give you positive integers.
^[^-]\d+((,([^-]\d+))?)+$
will give you only the lists where all the values are positive integers
For a list with any positive integer (but all valid integers negative or positive) I thinks this will check out:
^((-\d+,)?)+[^-]\d+((,([^-]\d+))?|(,-\d+)?)+$
Here's a great site for Regular Expressions:
http://gskinner.com/RegExr/
I use it all the time for testing live.

Related

How do I create a SELECT conditional in MySQL where the conditional is the character length of the LIKE match?

I am working on a search function, where the matches are weighted based on certain conditions. One of the conditions I want to add weight to is matches where the character length of the query string in a LIKE match is longer than 4.
This is what I want to the query to look like, roughly. %s is meant to represent the actual match found by LIKE, but I don't think it does. I'm wondering if there is a special variable in MySQL that does represent the precise character match found by LIKE.
SELECT help.*,
IF(CHAR_LENGTH(%s) > 4, 2, 0) w
FROM help
WHERE (
(title LIKE '%this%' OR title LIKE '%testy%' OR title LIKE '%test%') OR
(content LIKE '%this%' OR content LIKE '%testy%' OR content LIKE '%test%')
) LIMIT 1000
edit: I could in the PHP split the search string array into two arrays based on the character length of the elements, with two separate queries that return different values for 'w', then combine the results, but I'd rather not do that, as it seems to me that would be awkward, messy, and slow.
Check out FULLTEXT as another way to discover rows. It will be faster, but won't address your question.
This probably has the effect you want.
SELECT ....
IF ( (title LIKE '%testy%' OR
content LIKE '%testy%'), 2, 0)
....
Note that the "match" in your LIKEs includes the %, so it is the entire length of the string. I don't think that is what you wanted.
REGEXP "(this|testy|that)" will match either 4 or 5 characters (in this example). It may be possible to do something with REGEXP_REPLACE to replace that with the empty string, then see how much it shrank.
I think the answer to my question is that what I wanted to do isn't possible. There is no special variable in MySQL representing the core character match in a WHERE condtional where LIKE is the operator. The match is the contents of the returned data row.
What I did to reach my objective was took the original dynamic list of search tokens, iterated through that list, and performed a search on each token, with the SQL tailored to the conditions that matched each token.
As I did this I built an array of the search results, using the id for the database row as the index for the array. This allowed me to perform calculations with the array elements, while avoiding duplicates.
I'm not posting the PHP code because the original question was about the SQL.

How to find words by partially strings

I have been trying to solve this problem for hours, but I dont know how to approach it, so I would need a push to a right direction.
I want to create a page where users can find the appropriate word, by providing word length and characters.
For example, user wants to find all the 5 letter words, where the second letter is R and fourth V, like this:
_R_V_
I have a table with column WORDS with words "letter", "moon", "drive", "mrive" and the query should return: "drive" and "mrive".
Is it possible to do it in MySQL?
While I was looking for the direction I found that I should create a trie structure. I dont know how to do that, but I will learn it if there is no easier way.
Yes, you can use LIKE :
SELECT * FROM YourTable t
WHERE t.word_col LIKE '_R_V_'
_ Wildcard stands for any single character. This will also force the string to be 5 characters in length, since % wildcard is not used.
You can find a great explanation about LIKE wildcards in the link above.

How to make this regex in mysql?

A few min ago I found out that mysql accepts regex, and is great becouse I think it can solve my problem, but I don't know how to write it. So, I need something like this:
SELECT name FROM products WHERE name REGEXP 'regex code'
To give a little more explanation, the name must be in this format: 123425HT and not string99-123425HT. The 123425HT and string99-123425HT is taken arbitrary
Please Help, thanks!
Something like that:
SELECT * FROM t2 WHERE str REGEXP "^([0-9]+)([a-zA-Z]{2})$";
This regexp will found strings which starting from any digits and two small or big characters, for example:
123123hd
12345435MF
6572Sg
If you want use only 6 digits change from [0-9]+ to [0-9]{6}

Determine how many characters in text are within another text entry in MySQL

I have a table where there is a column of type 'text'. I know I can compare two entries to see is they are the same using a simple select statement. Is there a way to compare two entries and return how similar they are? More specifically, can it say how many characters are different between the two?
For example, suppose one entry is:
This is a line.
And another that is:
This is a line. And another.
I believe I can write a select statement that says the first in contained in the second. But is there a way to alert me that the first is in the second AND there are 15 extra characters in the 2nd?
Try to use Levinshtein distance http://www.artfulsoftware.com/infotree/queries.php#552
You can use LENGTH along with LIKE to do this. E.g.:
INSERT INTO test VALUES("HELLO WORLD");
select LENGTH(name)-length("HELLO") from test where name like "%HELLO%";
So you'd need to programmatically replace HELLO with whatever the string was you wanted to search for.
Is that what you were looking for?
You could simply measure the length of both as strings with char_length() and subtract the difference?
MySQL: char_length()
(Note that length() and char_length() return different values!)

Regexp MySql- Only strings containing two words

I have table with rows of strings.
I'd like to search for those strings that consists of only
two words.
I tried few ways with [[:space:]] etc but mysql was returning
three, four word strings also
try this:
select * from yourTable WHERE field REGEXP('^[[:alnum:]]+[[:blank:]]+[[:alnum:]]+$');
more details in link :
http://dev.mysql.com/doc/refman/5.1/en/regexp.html
^\w+\s\w+$ should do well.
Note; what I experience more often in the last days is that close to nobody uses the ^$-operators.
They are absolutely needed if you want to tell if a string starts or ends with something or want to match the string exactly, word for word, as you. "Normal" strings, like you used (I assume you used something like \w[:space]\w match in the string, what means that they also match if the condition is true anywhere within the string!
Keep that in mind and Regex will serve you well :)
REGEXP ('^[a-z0-9]*[[:space:]][a-z0-9]*$')