Mike
Mike

Reputation: 197

Regex to match alphanumeric -- query rows that contain numbers in postgresql

I want to write a regex that matches string that has both numeric and alphabets. ^[a-zA-Z0-9]*$ -- this pattern returns numeric as well as alphabets, but I want only alphanumeric. Basically I'm using postgresql to query rows that contain numericals in it.

Upvotes: 4

Views: 12528

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269873

I think multiple regex's is the easiest way:

where col ~ '^[a-zA-Z0-9]*' and
      col ~ '[0-9]' and
      col ~ '[a-zA-Z]'

There is probably a complicated regexp that combines this all together, but this seems like the most intuitive method.

Upvotes: 7

Martin Brandl
Martin Brandl

Reputation: 58931

This should work:

(?:[a-zA-Z]\d)|(?:\d[a-zA-Z])

Regular expression visualization

Upvotes: 1

Related Questions