Regular Expressions in ABAP ( Part 1 ) - email Validation

Many String operations are difficult to manage one example is the validation of email addresses. this could be easily done with the help of Regular Expressions. 

Invented by american mathematician Stephen Cole Kleenewho helped lay the foundations for theoretical science, regular expressions are a powerful tool for string processing. They are a sequence of characters that form a search pattern, which are used for string or pattern matching within strings.

An email address is generally recognized as having two partes separated by a "At sign" (@). By itself this serves as a basic email address validation. 
If you are interested in a more robust form of validation you can validate the email address according to the RFC 822 grammar (http://www.ietf.org/rfc/rfc0822.txt?number=822), which is a publication of the Internet Engineering Task Force (IETF) and the Internet Society, the principal technical development and standards-setting bodies for the Internet.

Bellow is a simple example of an ABAP email validation program, using the power of Regular Expressions (REGEX).

REPORT  z_regex_email.

SELECTION-SCREEN BEGIN OF SCREEN 100.
PARAMETERS email TYPE c LENGTH 30 LOWER CASE.
SELECTION-SCREEN END OF SCREEN 100.

*----------------------------------------------------------------------*
*       CLASS lcl_demo DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_demo DEFINITION.

  PUBLIC SECTION.
    CLASS-METHODS class_constructor.
    CLASS-METHODS main.
  PRIVATE SECTION.
    CLASS-DATA: mv_email_regex TYPE string.

ENDCLASS.                    "public SECTION.

*----------------------------------------------------------------------*
*       CLASS lcl_demo IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_demo IMPLEMENTATION.
  METHOD class_constructor.

    " Note that this regular expression is by no means bulletproof, as
    " it is extrmely dificult to create such regular expression but it
    " covers most of the cases.
    " 
    mv_email_regex =  '\w+(\.\w+)*@(\w+\.)+((\l|\u){2,4})'.

  ENDMETHOD.                    "class_constructor
  METHOD main.

    CALL SELECTION-SCREEN 100.

    " We use the predifined string function MATCH
    " ( http://help.sap.com/abapdocu_702/en/abenmatch_functions.htm )
    IF matches( val   = email
                regex = mv_email_regex ).
      " The Email Matches the Regular expression
      MESSAGE 'Format matches' TYPE 'S'.
    ELSE.
      " There is no match between the email and the regular expression
      MESSAGE 'Format does not Match!' TYPE 'S' DISPLAY LIKE 'E'.
    ENDIF.

  ENDMETHOD.                    "main

ENDCLASS.                    "lcl_demo IMPLEMENTATION

START-OF-SELECTION.
  lcl_demo=>main( ).

No comments:

Post a Comment