How to write a function with Oracle regexp to test a domaine name?
I would like to w开发者_Go百科rite a PLSQL function that returns true if the domain name I pass is valid.
I should use regular expression, but I don't know how to do this.
declare
ignore boolean;
begin
isDomainSyntaxOk('www.laclasse.com'); --> should return true.
isDomainSyntaxOk('www.la classe.com'); --> should return false because of the space char.
end;
Any ideas ?
My regex skills are weak, so I'm hoping that someone comes along and fixes this:
create or replace
FUNCTION IS_VALID_DOMAIN (p_DOMAIN IN VARCHAR2) RETURN BOOLEAN IS
BEGIN
RETURN REGEXP_LIKE(p_DOMAIN, '^[a-z0-9][a-z.0-9]*[a-z]$');
end;
Tanks for Adam Hawkes' idea. I 've found some regExp that does what I want. The function should return true in these cases :
- only alphanum chars.
- accept '.' and '-' but not at first char and not at last char.
It's should be like this :
function isSyntaxeDomaineOk(pDomain varchar2) return boolean is
begin
return regexp_like(pDomain, '^[a-z0-9][-a-z.0-9]*[a-z0-9]$');
end isSyntaxeDomaineOk;
I finally modified my function to deal with the '..' occurence problem :
function isSyntaxeDomaineOk(pStr varchar2, pChar4Space varchar2 default null) return boolean is
begin
return regexp_like(pStr, '^[a-z0-9][-a-z.0-9]*[a-z0-9]$')
and not regexp_like(pStr, '\.\.');
end isSyntaxeDomaineOk;
HTH.
精彩评论