Jump to content


Photo

Regex Help

mysql regex

  • Please log in to reply
3 replies to this topic

#1 Backslider

Backslider

    Member

  • Members
  • PipPip
  • 18 posts

Posted 26 November 2012 - 04:54 PM


I need to find valid records as part of a database cleanup script.

What I need is a regular expression that will match, for a single column:

records containing BOTH numbers AND letters (or any other character).
records containing ONLY numbers (including decimals)

Invalid records are those which contain ONLY letters and I do not want these returned, or NULL for the column.

Anybody good with regex?


#2 requinix

requinix

    Hopeless Member

  • Moderators
  • 5,913 posts
  • LocationWA

Posted 26 November 2012 - 06:22 PM

Maybe I'm misreading it but your two conditions are simply "record contains numbers and other stuff OR record contains numbers and not other stuff". Which is really just saying that it has to contain a number.

#3 Backslider

Backslider

    Member

  • Members
  • PipPip
  • 18 posts

Posted 26 November 2012 - 08:59 PM

Hi, thanks for the reply. I actually solved this as follows:

SELECT model
FROM products
WHERE model !=  ''
AND model !=  '0'
AND (model REGEXP  '[0-9][a-z]' OR model REGEXP  '[0-9]')


#4 requinix

requinix

    Hopeless Member

  • Moderators
  • 5,913 posts
  • LocationWA

Posted 26 November 2012 - 09:49 PM

Right. And what I'm saying is that model REGEXP "[0-9]" will also include the model!="" (contains something) and model REGEXP "[0-9][a-z]" (contains a number followed by a letter) cases. They're redundant. Still need the !=0 though.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com