Validate email address using regular expression in SAP ABAP

Author | Last Updated| | 5 Comments Facebook Share Twitter Share Google Plus Share A+ A-


Validate email address using a common regular expression in SAP ABAP programming

Step1:Validate email address using regular expression in SAP ABAP Normal Top^

SAP ABAP- Validate email address using a regular expression, by using this we can not validate the existance of email address but we can validate uviversal email format.

Resourse used : SAP class CL_ABAP_REGEX and CL_ABAP_MATCHER .

Regular expression for email validation

'^[_a-zA-Z0-9-] (\.[_a-zA-Z0-9-] )*@[a-zA-Z0-9-] (\.[a-zA-Z0-9-] )*(\.[a-zA-Z]{2,4})$' 
Declare classes
DATA:GO_REGEX    TYPE REF TO CL_ABAP_REGEX,
     GO_MATCHER  TYPE REF TO CL_ABAP_MATCHER.
Add regular expression.
DATA: L_PAT(500) TYPE C.

L_PAT = '^[_a-zA-Z0-9-] (\.[_a-zA-Z0-9-] )*@[a-zA-Z0-9-] (\.[a-zA-Z0-9-] )*(\.[a-zA-Z]{2,4})$' .
Create object for regex class
CREATE OBJECT GO_REGEX
  EXPORTING
    PATTERN     = L_PAT
    IGNORE_CASE = ABAP_TRUE.
Create macher and check match
GO_MATCHER = GO_REGEX->CREATE_MATCHER( TEXT =  'Emailid' ). "email to validate
IF GO_MATCHER->MATCH( ) IS INITIAL.
  WRITE:/ 'Email is not valid'.
ELSE.
  WRITE :/ 'Email is valid'.
ENDIF.

Example program for validating email

REPORT ZSAPN_VALIDATE_EMAIL.
PARAMETERS P_EMAIL TYPE STRING.
DATA:GO_REGEX    TYPE REF TO CL_ABAP_REGEX,
     GO_MATCHER  TYPE REF TO CL_ABAP_MATCHER.

DATA: L_PAT(500) TYPE C.

L_PAT = '^[_a-zA-Z0-9-] (\.[_a-zA-Z0-9-] )*@[a-zA-Z0-9-] (\.[a-zA-Z0-9-] )*(\.[a-zA-Z]{2,4})$' .

CREATE OBJECT GO_REGEX
  EXPORTING
    PATTERN     = L_PAT
    IGNORE_CASE = ABAP_TRUE.
TRANSLATE P_EMAIL TO LOWER CASE.
GO_MATCHER = GO_REGEX->CREATE_MATCHER( TEXT =  P_EMAIL )."lt_node_op4 ).
IF GO_MATCHER->MATCH( ) IS INITIAL.
  WRITE:/ 'Email is not valid'.
ELSE.
  WRITE :/ 'Email is valid'.
ENDIF.

Tutorial Comments

Total Comments: Add your Comment
29 Mar 2018

I have been testing the regular expression and it seems that it doesn't work... Maybe with --> L_PAT = '\w (\.\w )*@(\w \.) (\w{2,4})'. Regards, Rafa

29 Mar 2018

At my standard system this pattern works: L_PAT = '\w+(\.\w+)*@(\w+\.)+(\w{2,4})'.

14 Apr 2020

Quicker and Simpler than the SBA, http://MySmallBusinessFundingNow.info?url=sapnuts.com can get your business a loan for $2K-350,000 With low-credit and without collateral. Use our short form to See exactly how much you can get, No-Cost: http://MySmallBusinessFundingNow.info?url=sapnuts.com If you've been established for at least one year you are already pre-qualified. Our Quick service means funding can be completed within 48hrs. Terms are personalized for each business so I suggest applying to find out exactly how much you can get. This is a free service from a qualified lender and the approval will be based on the annual revenue of your business. Funds are also Non-Restrictive, allowing you to use the full amount in any way including bills, taxes, hiring, marketing, expansion, or Absolutely Any Other expense. There are limited SBA and private funds available so please apply now if interested, Click Here: http://MySmallBusinessFundingNow.info?url=sapnuts.com Have a great day, The My Small Business Funding Now Team remove here - http://mysmallbusinessfundingnow.info/r.php?url=sapnuts.com&id=e101

27 Apr 2020

THX @jankovarr !!!! your pattern works for me !!

Leave a Comment

Enter Comment Text