Search code examples
regexoracle-databaseplsqlpreg-match

Oracle regexp_like - only certain characters, numbers and one symbol


SELECT count(*)
FROM dual
WHERE regexp_like ('ABC-123', '^[a-zA-Z0-9]*$');

I would like to use Oracle's regexp_like to only allow the following:

  1. A to Z, uppercase and lowercase.
  2. All numbers.
  3. Symbol -

Solution

  • If you plan to match a hyphen, the ones inbetween a-z, A-Z and 0-9 are not literal hyphens, they are functional characters defining a range.

    You need to add a hyphen to the end of the [...]:

    ^[a-zA-Z0-9-]*$
               ^
    

    To avoid empty matches, use

    ^[a-zA-Z0-9-]+$
                 ^