开发者

MySQL REGEXP matching positive numbers

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 mat开发者_开发技巧ches 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜