2

I'm using REGEXP to match specific strings out of a ton of text in a LONGTEXT column. For example:

Text text text text text
 text text text text text
 text text SOLD: 75/101 text
 text text text text text

So my queries have been looking like this:

SELECT * FROM `test` WHERE `file` REGEXP
'SOLD:[ ]{1}[0-9]{1,2,3}/[0-9]{1,2,3}'

Which would properly match the SOLD: 75/101 string.

But is it possible, to do a comparison on the numerator? Such as, find all SOLD: >=75/101?

The comparison number would be a user inputted number via $_POST. I know that REGEXP isn't really meant for comparing numbers, but is there some way to maybe capture the string with regexp, then perform a comparison on the integers some other way?

3
  • You can create stored procedure to encapsulate any logic Commented Sep 23, 2012 at 20:10
  • Would you only have to consider fractions like 76/101 and 123/101, or would you also have to take 70/80 into account (which is greater than 75/101, of course)? Commented Sep 23, 2012 at 21:09
  • @TimPietzcker Yes, I'd have to consider those first two also. I'm not interested in comparing the fraction as a whole, just the numerator. Commented Sep 23, 2012 at 21:13

1 Answer 1

3

If you only have to look at the numerator, as stated in your comment, then

SELECT * FROM mytable WHERE mycolumn REGEXP 
"SOLD: ([1-9][0-9]{2,}|[8-9][0-9]|7[5-9])/101";

should work.

By the way, [0-9]{1,2,3} is a syntax error in most regex flavors (I don't know how MySQL handles it); the correct way to specify a range is [0-9]{1,3}.

Sign up to request clarification or add additional context in comments.

3 Comments

What is the user inputted $_POST value was 80 or 90 though?
Ah, right. I hadn't read that part thoroughly enough. Sorry, MySQL regexes don't support capturing groups, so I guess you really have to use a custom function for that. Unfortunately, I don't know MySQL well enough to suggest a solution.
@Norse: That means "2 or more".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.