0

I had to search a string in query like this 5X115, but there were possibilities that a string could appear like 5X110/115 ( this is equal to 5X110, 5X115 ) so i wrote this REGEXP

'^5X[0-9]{1,}.?[0-9]{1,}(/115)|5X115'

and it worked fine, now i have to modify this, i have to use a range here, searching between 5X114.3 and 5X115.7 ( subtracted and added 0.7 to it ) Now how can i make this in one expression to search it with in ranges? I have tried to make so many combinations but i was confined to generate this string in the end:

[0] => '^5X[0-9]{1,}.?[0-9]{1,}(/114.3)|5X114.3
3. [1] => '^5X[0-9]{1,}.?[0-9]{1,}(/114.4)|5X114.4
4. [2] => '^5X[0-9]{1,}.?[0-9]{1,}(/114.5)|5X114.5
5. [3] => '^5X[0-9]{1,}.?[0-9]{1,}(/114.6)|5X114.6
6. [4] => '^5X[0-9]{1,}.?[0-9]{1,}(/114.7)|5X114.7
7. [5] => '^5X[0-9]{1,}.?[0-9]{1,}(/114.8)|5X114.8
8. [6] => '^5X[0-9]{1,}.?[0-9]{1,}(/114.9)|5X114.9
9. [7] => '^5X[0-9]{1,}.?[0-9]{1,}(/115)|5X115
10. [8] => '^5X[0-9]{1,}.?[0-9]{1,}(/115.1)|5X115.1
11. [9] => '^5X[0-9]{1,}.?[0-9]{1,}(/115.2)|5X115.2
12. [10] => '^5X[0-9]{1,}.?[0-9]{1,}(/115.3)|5X115.3
13. [11] => '^5X[0-9]{1,}.?[0-9]{1,}(/115.4)|5X115.4
14. [12] => '^5X[0-9]{1,}.?[0-9]{1,}(/115.5)|5X115.5
15. [13] => '^5X[0-9]{1,}.?[0-9]{1,}(/115.6)|5X115.6
16. [14] => '^5X[0-9]{1,}.?[0-9]{1,}(/115.7)|5X115.7

But i need a single line REGEXP, if anyone can help?

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
user3118004
  • 35
  • 1
  • 9
  • So basically your question is for a regular (sub-)expression to match "4.3", "4.4, "4.5",..., "5.7"? – JimmyB Apr 10 '14 at 14:58
  • strings can appear like this in database 5x114/115/100, it means 5 applies to every digit after '/', but now i need a REGEXP that if provided '5x114' should search string from the above combination and also 0.7- and 0.7+ to it as well – user3118004 Apr 10 '14 at 15:01
  • @HannoBinder this works well "'^5X[0-9]{1,}.?[0-9]{1,}(/115)|5X115'" for searching "5x114/115/100" OR "5x115" OR "5x115/100" anything, but :( i wanna check if REGEXP works for ranges as well – user3118004 Apr 10 '14 at 15:03
  • Yes. And the range you mentioned was that of those numbers that end in "4.3", or "4.4", or "4.5", or ..., or "5.7", right? - That would roughly translate to `(4\.[3-9])|(5\.[0-7])`. Can you take it from here? – JimmyB Apr 10 '14 at 15:05
  • By the way, the more robust and flexible solution would of course be to extract all numerical substrings and have them converted to `FLOAT` for example, so you can perform normal arithmetic operations and comparisons on them. – JimmyB Apr 10 '14 at 15:08
  • FYI: There is a link to the [official MySQL regex documentation](https://dev.mysql.com/doc/refman/5.1/en/regexp.html) in the [Stack Overflow Regular Expression FAQ](http://stackoverflow.com/a/22944075/2736496), listed under "General documentation > official docs for specific flavors". Also, although it's overkill for your particular needs here, you may also be interested in the answer [using regex to ensure a number falls within a min-max range (such as 1-31)](http://stackoverflow.com/a/22131040/2736496), which is listed under "Common Validation Tasks > Numeric". – aliteralmind Apr 10 '14 at 15:21

0 Answers0