4

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.

3
  • 1
    Can you give examples of what you want to match and not match? Commented Aug 14, 2016 at 18:07
  • Always only alpha numeric but never foreign characters or spaces? Commented Aug 15, 2016 at 9:32
  • Related: stackoverflow.com/questions/59309953/… contains solution with ~ '[[:alpha:]]' for Unicode codepoints. Commented Sep 24 at 5:25

2 Answers 2

7

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.

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

Comments

1

This should work:

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

Regular expression visualization

1 Comment

This will match a string that contains any other character also, spaces and special characters. All you're validating is that it contains #Char or Char# is contained in the column.

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.