Close Sidebar close
Sapignite
  • SAP
  • Coding Challenges
  • Download
  • SAP Ignite Products

Subscribe & Follow

All product names on this web site are trademarks of the companies that own them. Sapignite.com is not affiliated with SAP AG in any way. SAP AG is the registered trademark holder of SAP, SAP R/3, mySAP, ABAP, xApps, NetWeaver, and other proprietary terms. The technical information on this site is verified to the greatest extent possible, however, any information found on this site is used at the site visitor's own risk. Sapignite.com reserves the right to correct any errors or omissions in any portion of this site at any time without obligation.

  • SAP
  • Coding Challenges
  • Download
  • SAP Ignite Products
Sapignite
Sapignite
    ABAP

    Regex in ABAP

    July 21, 2017

    Regular Expressions (Regex) and its use in ABAP

    Regular Expressions (Regex) is far from new, even in ABAP. ABAP supports regular expressions as of Release 7.00, released to customers on Oct 24, 2005. Although not so new its use is rarely found in ABAP. One reason is that SAP has already provided other ways to search for patterns e.g. SEARCH txt FOR pattern and IF txt CP pattern.

    But what is Regex after all?

    Wikipedia

    “In computing, a regular expression also referred to as regex or regexp, provides a concise and flexible means for matching strings of text, such as particular characters, words, or patterns of characters. A regular expression is written in a formal language that can be interpreted by a regular expression processor, a program that either serves as a parser generator or examines text and identifies parts that match the provided specification.”

    There is a very good tutorial about Regex at http://www.regular-expressions.info. We will skip the basics of Regex and go directly to its use in ABAP.

    Using Regex in ABAP

    Regex are extremely more powerful than traditional SAP patterns and is commonly used for searching and validating text. ABAP supports Regex in the statements FIND and REPLACE and via the the classes CL_ABAP_REGEX and CL_ABAP_MATCHER. There is an excellent article at SDN produced by Shaira Madhu with many more details of Regex in ABAP.

    Let’s see some examples.

    Example 1 – Tests if text starts with Hello (case sensitive)

    DATA text TYPE string.
    DATA moff TYPE i.
    DATA mlen TYPE i.

    text = `Hello World example`.

    FIND REGEX ‘^Hello’ IN text MATCH OFFSET moff
    MATCH LENGTH mlen.
    IF sy-subrc = 0.
    WRITE / text+moff(mlen).
    ENDIF.

    Example 2 – Removes example from the text only if it is the last word

    DATA text TYPE string.

    text = `Hello World example`.

    REPLACE REGEX ‘example$’ IN text WITH ”.

    IF sy-subrc = 0.
    WRITE / text.
    ENDIF.

    Example 3 – Validates e-mail using the class CL_ABAP_MATCHER

    DATA email   TYPE string VALUE `webmaster@sapignite`. “missing .com

    DATA matcher TYPE REF TO cl_abap_matcher.

    matcher = cl_abap_matcher=>create(
    pattern = `\w+(\.\w+)*@(\w+\.)+(\w{2,4})`
    text    = email ).

    IF matcher->match( ) IS INITIAL.
    MESSAGE ‘Invalid e-mail’ TYPE ‘I’ DISPLAY LIKE ‘E’.
    ENDIF.

    Tip: For testing Regex without coding you can use the ABAP program DEMO_REGEX_TOY.

    Below are listed some links for those who want to start using Regex.

    SDN

    Regular Expression Processing in ABAP
    http://www.sdn.sap.com/irj/scn/index?rid=/library/uuid/902ce392-dfce-2d10-4ba9-b4f777843182

    Regex Toy- Testing Regular Expressions In ABAP
    http://www.sdn.sap.com/irj/scn/index?rid=/library/uuid/1f9cd701-0b01-0010-87b8-f86f9b7b823f

    ABAP Geek 14 – Regular Expressions Made Easy
    http://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/15768

    Regular Expression in ABAP Book
    http://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/23476

    • Consuming a web service in ABAP

    • X-Ray Eye to find all enhancements in SAP !!

    You Might Also Like

    Create a web service in 10 minutes

    July 3, 2013

    My journey to ABAP Certification

    July 20, 2017

    SAP Coding Challenge – IV

    July 8, 2017

    No Comments

    Leave a Reply Cancel Reply

Sign up for some exciting content

Recent Posts

  • How to prepare for SAP IBP Certification
  • What is SAP Leonardo ?
  • RoadMap UI Tutorial webdynpro for ABAP
  • Offline Adobe forms in WebDynPro for ABAP
  • MVC Architecture for Webdynpro for ABAP

Categories

  • ABAP
  • Android
  • Coding Challenges
  • Enhancement
  • featured
  • Interviews
  • Learning Hub
  • PI ( Process integration )
  • SAP
  • SAP HANA
  • Uncategorized
  • Webdynpro for ABAP
  • Webdynpro For ABAP