ABAP — : Using Regex / Regular Expressions in a Pre-Netweaver Release

Actually the new SAP Releases (starting with Basis Release 6.40) support regexs in ABAP in REPLACE and SEARCH directives. But as there are so many Pre-Netweaver Releases as <= 6.20 you may want to use a regular expression or you have the need to do so. (I for myself have to use it quite often, as i loved it to use in perl and if you got the grip on it you don’t want to miss it). So for you poor guys – there is a opportunity to do so. Just use the build in Javascript-Engine. I now it’s quite awkward but faster as you may think. Just paste the following code into your application environment and build a class with a static method: Have Fun!
And for those of you ABAP-Coders and anybody who want to learn a bit good coding ( use REGEX!) – you found the right way to do so here

*Copyright (c) 2008, J.Rumpf, www.web-dreamer.de / BSD Licence
*All rights reserved.
*
*Redistribution and use in source and binary forms, with or without modification
*are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, th
* is list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of the owner nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific
* prior written permission.
*
*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
*ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
*WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
*DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
*FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
*DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
*SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
*OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
*OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*&———————————————————————*
*& Report ZREGEX
*&
*&———————————————————————*
REPORT zregex.
**(c) by J. Rumpf
* Defintion of ZTMATCH
*Matching-Table of part matches of brackets
TYPES:
BEGIN OF zlmatch,
comp TYPE string,
END OF zlmatch,
ztmatch TYPE TABLE OF zlmatch.
DATA source TYPE string.

* Defintion of Class in Report
* Important: Definitio has to be at the earliest point in your report!
CLASS z_tools DEFINITION.
PUBLIC SECTION.
* The Parameter definition of the Method has to be at the definition
* CLASS-METHODS means Static Method / instance Method
has to be declared as ”METHODS”
CLASS-METHODS regex
EXPORTING
lastindex TYPE i
leftcontext TYPE string
rightcontext TYPE string
index TYPE i
found TYPE boolean “boolean variable (X=true, -=false, space=unknown)
match TYPE ztmatch “For use with regular expressions
- the definition of table ZTMATCH see below.
return_value TYPE string
error_message TYPE string
CHANGING
searchstring TYPE string “string to be regex applicated
modifier TYPE string DEFAULT ” “/gims/
regex TYPE string DEFAULT ” “regular expression
.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
**********************************************************************
**Test on Regex
DATA return_value TYPE string.
DATA: match TYPE ztmatch, “Regex Table
wa_match like LINE OF match,
lastindex TYPE i, “as it says last occurence of match
leftcontext TYPE string,
rightcontext TYPE string,
index TYPE i,
searchstring TYPE string, “String to search in
modifier TYPE string,
regex TYPE string, “your Regex
found TYPE boolean,
error_message TYPE string.

regex = ‘b+(a)*(b+)’. “Example
searchstring = ‘abbbbabbaa’. “
modifier = ”.

CALL METHOD z_tools=>regex
IMPORTING
lastindex = lastindex
leftcontext = leftcontext
rightcontext = rightcontext
index = index
found = found
match = match
return_value = return_value
error_message = error_message
CHANGING
searchstring = searchstring
modifier = modifier
regex = regex.
WRITE / ‘Errormessage:’.
write / error_message.
WRITE / ‘Found:’.
WRITE / found.
WRITE / ‘MatchParts:’.
LOOP at match INTO wa_match.
WRITE / wa_match-comp.
ENDLOOP.
**********************************************************************
* IMPORTANT !!! Implementation has to be at END of your REPORT
**********************************************************************
CLASS z_tools IMPLEMENTATION.
* Implementation of Regex Method.
METHOD regex .
**(c) by J. Rumpf
DATA source TYPE string.

DATA js_processor TYPE REF TO cl_java_script.
js_processor = cl_java_script=>create( ).
**********************************************************************
*Start of JavaScript–>ABAP Mapping of Variables
js_processor->bind(
EXPORTING
name_obj = ‘ ’
name_prop = ‘regex’ “Variable in Javascript-Code
CHANGING
data = regex “Variable in Abap-Code
).
js_processor->bind(
EXPORTING
name_obj = ‘ ’
name_prop = ’searchstring’
CHANGING data = searchstring
).
js_processor->bind(
EXPORTING
name_obj = ‘ ’
name_prop = ‘modifier’
CHANGING data = modifier
).
js_processor->bind(
EXPORTING
name_obj = ‘ ’
name_prop = ‘index’
CHANGING
data = index
).
js_processor->bind(
EXPORTING
name_obj = ‘abap’
name_prop = ‘match’
CHANGING
data = match
).
js_processor->bind(
EXPORTING
name_obj = ‘ ’
name_prop = ‘lastindex’
CHANGING
data = lastindex
).
js_processor->bind(
EXPORTING
name_obj = ‘ ’
name_prop = ‘leftcontext’
CHANGING
data = leftcontext
).
js_processor->bind(
EXPORTING
name_obj = ‘ ’
name_prop = ‘rightcontext’
CHANGING
data = rightcontext
).
js_processor->bind(
EXPORTING
name_obj = ‘ ’
name_prop = ‘found’
CHANGING
data = found
).
**********************************************************************
DATA: wa LIKE LINE OF match. “wa = workarea of table ZTMATCH
wa-comp = ‘ ’.
APPEND wa TO match.
**********************************************************************
*JavaScript Code REGEX to evaluate/run later on.
CONCATENATE
‘var re = new RegExp(regex, modifier);’

‘var m = re.exec(searchstring);’
‘ if (m == null) {’
‘ found = false;’
‘ } else {’
‘ found = true; ’
‘ index = m.index;’
‘ lastindex = m.lastIndex;’
‘ leftcontext = m.leftContext;’
‘ rightcontext = m.righContext; ’
‘ var len = abap.match.length;’
‘ for (i = 0; i < m.length; i++) ’
‘{’
‘ abap.match[len-1].comp = m;’
‘ abap.match.appendLine();’
‘ len++;’
‘ }’
‘}’
INTO source SEPARATED BY cl_abap_char_utilities=>cr_lf.

return_value = js_processor->evaluate( source ). “ Call JavaScript
error_message = js_processor->last_error_message. “ Get Errormessages
ENDMETHOD. “REGEX
ENDCLASS. “z_tools IMPLEMENTATION

ABAP — : Using Regex / Regular Expressions in a Pre-Netweaver Release

6 Gedanken zu „ABAP — : Using Regex / Regular Expressions in a Pre-Netweaver Release

  1. Naimesh Patel schreibt:

    Hello Joe,

    Nice to see the workaround using the JavaScript engine in ABAP kernel. We can very well use this JS engine for other purposes as well like complex calculations Case Study: JavaScript in ABAP Usage. But it seems SAP is going to remove CL_JAVA_SCRIPT as per the tweet from Uwe Fetzer. Only the future releases can confirm this….

    Regards,
    Naimesh Patel

  2. johannes schreibt:

    Hi Manfred, der erste Fehler liegt daran das er REGEX in der DEFINITION nicht erkennt. Wahrscheinlich ist die Klassendefinition nicht am Anfang des Reports. Der zweite Fehler ist in der tat auf den ersten zurückzuführen.

  3. Manfred schreibt:

    Vielen Dank für die Mühe Joe

    Ich habe nun eine neue Klasse z_tools erzeugt und deinen Code in eine Methode regex gepasted…

    Den Code über “ CLASS z_tools IMPLEMENTATION.“ habe ich in einen Report gepackt…

    Bei der überprüfung erhalte ich noch 2 Fehler:
    Klasse z_tools,Methode REGEX
    Feld „REGEX“ unbekannt. Es ist weder in einer der angegebenen Tabellen
    enthalten noch durch eine „DATA“-Anweisung definiert.

    und beim Report:
    Programm z_pma_unames
    Zu „CLASS Z_TOOLS DEFINITION“ fehlt „CLASS Z_TOOLS IMPLEMENTATION“.

    hängt wohl aber damit zusammen, dass schon die Klasse reklamiert.

    Werde nun mal weiter tüfteln.
    Vielen Dank nochmals…

  4. Manfred schreibt:

    Hallo Joe

    Ich bin neu in ABAP wäre aber sehr interessiert daran wie man Regular Expressions in ABAP Programmen benutzen kann.

    Welche Teile des obigen Codes gehören in was für Elemente? (Klassen, Programme usw…)

    Ich währe sehr froh um eine kleine Anleitung, habe früher viel mit TCL gemacht und auf Regexp’s möchte ich seither nicht mehr verzichten.

    Gruess und vielen Dank

Schreibe einen Kommentar

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.