Forum Stats

  • 3,876,217 Users
  • 2,267,082 Discussions
  • 7,912,473 Comments

Discussions

regex validation: valid matches not found

j4john
j4john Member Posts: 196 Blue Ribbon
edited Oct 5, 2006 5:09AM in APEX Discussions
Hi

I'm trying to use a regex to validate the format (not the content!) of email addresses. The best regex if found for this so far is
^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$
which I've tested using http://regexlib.com/RETester.aspx

Strings which produce matches using this tester fail validation when I use the same pattern as an item-level validation on an Apex page.

I know that there are some different regex dialects out there, so maybe that's the problem...

Can anybody help out with an explanation - or even a fix :) ?

Many thanks

john

Comments

  • Chet Justice
    Chet Justice Member Posts: 574 Bronze Badge
    I am not familiar with regex but I did read this great post the other day on the SQL/PLSQL forum by cd.

    429716

    If that doesn't help you, you'd probably get better help over there (hopefully from cd himself).

    chet
  • j4john
    j4john Member Posts: 196 Blue Ribbon
    Thanks Chet,

    Hadn't spotted cd's thread (on which I've now created a cross-ref to this one).

    john
  • cd_2
    cd_2 Member Posts: 5,021
    edited Oct 5, 2006 3:31AM
    The problem seems to be a missing "*" metacharacter. I've put a small test case together:
    WITH t AS (SELECT '[email protected]' email
                 FROM dual
                UNION 
               SELECT '[email protected]' 
                 FROM dual
              )
    SELECT t.email 
         , REGEXP_SUBSTR(t.email, '^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$') orig_check
         , REGEXP_SUBSTR(t.email, '^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z]*[-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$') new_check
      FROM t
      ;        
    I'm going to deal with inet specific values (IP4 addresses, emails, URLs) in the second part of my regex introduction.

    C.
  • j4john
    j4john Member Posts: 196 Blue Ribbon
    Thankyou C,

    Your fix does the trick.

    I still don't understand why I got different results from http://regexlib.com/RETester.aspx (assuming it is POSIX compliant) and Apex.

    I look forward to your next installment.

    jd
  • cd_2
    cd_2 Member Posts: 5,021
    edited Oct 5, 2006 5:09AM
    Maybe you should try another regex testing software? 2 out of 3 could give you a hint which implementation could have a bug.

    C.
This discussion has been closed.