1

Do you know if is possible to transform a pattern like this to regular expressions:

ABCDXXXXYYYY

Where ABCDEFGH.. are consecutive numbers and V, X, Y, Z are any number.

The pattern above should match:

123400006666 456799994444 etc.

Please note that I'm not asking for a full solution, but some idea on how to approach this problem. Have you ever faced a situation like this before (to search a DB for defined patterns that doesn't seem to fit RegExps?

Any comment would be really appreciated.

2 Answers 2

3

You can't identify consecutive numbers in a regular expression as they're too context dependant.

However, I think this would be easily possible in PL/SQL and possibly possible in SQL.

If you only want to use SQL then you can generate a string of consecutive numbers using a combination of connect by and either the undocumented function wm_contact or the user-defined function stragg

Something like:

 select replace(stragg(level),',','')
   from dual
connect by level <= 5

Concatenating this with a regular expression may get you close but I don't think that this is the way to go. I would definitely investigate using a PL/SQL function and possibly forgetting about regular expressions completely.

Doing the following will split out a number into an array, which you can then loop through and manipulate. As requested, this is just a starting point and you may want to change it around a lot. As there's no actual SQL and it's just string manipulation it's pretty efficient doing something like this.

create or replace function validate_phone( P_phone number ) 
                 return number is

   type t__phone is table of number index by binary_integer;
   t_phone t__phone;

   l_consecutive varchar2(150);

begin

   -- Test whether we actually have a number first ( code below ).
   if is_number(P_phone) = 0 then
       return null;
   end if;

   -- Split out the phone number into individual array elements.
   for i in 1 .. length(to_char(P_phone)) loop

      t_phone(i) := substr(to_char(P_phone, i, 1))

   end loop;

   for i in t_phone.first .. t_phone.last loop

      -- If we find a consecutive number then build this string.
      if t_phone.exists(i + 1)
        and t_phone(i) = t_phone(i + 1) - 1 then
         l_consecutive := l_consecutive || t_phone(i);
      end if;

   end loop;

   return something;

end validate_phone;

You may, as indicated in the above want to check whether your phone number is actually numeric first:

create or replace function is_number( P_number varchar2 ) 
        return number is

   /* Test a number to see whether it actually is one
      return a 1 / 0 rather than boolean so it can also
      be used in plain SQL.
      */

   l_number number;

begin

   l_number := P_number;

   return 1;

exception when others then
   return 0;
end is_number;
2

The language you describe is not context-free (in case the length of the prefix consisting of consecutive numbers is arbitrary) and not a regular language, therefore can not be expressed by a regular expression.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.