How to check zipcode using regex?

How to check zipcode using regex?

It should be in the following pattern: -

[A-z][0-9][A-Z] [0-9][A-Z][0-9]

eg.

B5D 2M4 
b5d 2m4 

I am using Oracle9i.

Thanks in advance, Shubhoval Ghosh

+5
source share
1 answer

Unfortunately, since you are using a very old version of Oracle, you cannot use standard regular expression functions such as REGEXP_LIKE. If you need to upgrade to a moderately late version of Oracle, this will be the way to go.

Oracle OWA_PATTERN. , OWA_PATTERN.MATCH BOOLEAN, SQL, PL/SQL. , , -, , , .

SQL> ed
Wrote file afiedt.buf

  1  declare
  2    function is_valid_zip( p_zip_code in varchar2 )
  3      return boolean
  4    is
  5    begin
  6      return owa_pattern.match( p_zip_code,
  7                                '[A-Z]{1}\d{1}[A-Z]{1}\d{1}[A-Z]{1}\d{1}',
  8                                'i' );
  9    end is_valid_zip;
 10  begin
 11    if( is_valid_zip( 'A1B2C3' ) )
 12    then
 13      p.l( '1) Match' );
 14    else
 15      p.l( '1) No match' );
 16    end if;
 17    if( is_valid_zip( '12345' ) )
 18    then
 19      p.l( '2) Match' );
 20    else
 21      p.l( '2) No match' );
 22    end if;
 23* end;
SQL> /
1) Match
2) No match

PL/SQL procedure successfully completed.
+4

All Articles