check if string has the format : 2 lettes au max following with numbers with regexp_like oracle

397 Views Asked by At

Could you please tell me the expression regexp_like to check if a string has the format which begins with one or two letters max followed by numbers. For example, like 'A25', 'AB567'.

Thanks

3

There are 3 best solutions below

0
On

How about

SQL> with test (col) as
  2    (select 'A25'      from dual union all
  3     select 'AB567'    from dual union all
  4     select 'A12A532A' from dual union all
  5     select 'ABC123'   from dual union all
  6     select '12XYZ34'  from dual
  7    )
  8  select col,
  9    case when regexp_like(col, '^[[:alpha:]]{1,2}[[:digit:]]+$') then 'OK'
 10         else 'Wrong'
 11    end result
 12  from test;

COL        RESULT
---------- ----------
A25        OK
AB567      OK
A12A532A   Wrong
ABC123     Wrong
12XYZ34    Wrong

SQL>
0
On

This match a string that contain only numbers and letters, that start with a letter, when there is never more than 2 letters in a row.

^(([A-Za-z]){1,2}([0-9])+)*\2{0,2}$
1
On

The regular expression would be ^[A-Z]{1,2}[0-9]+$.

Working demo on db<>fiddle here