设为首页收藏本站

SAP Best Business Solution

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 244|回复: 0

ZREGEX

[复制链接]

24

主题

24

帖子

197

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
197
发表于 2018-3-29 09:53:29 | 显示全部楼层 |阅读模式
*&#######################*
*& 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.                    "z_tools DEFINITION
**********************************************************************
**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
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



QQ|Archiver|手机版|小黑屋|www.sapbbs.com    

GMT+8, 2018-7-21 09:49 , Processed in 0.430485 second(s), 30 queries .

声明:本站严禁任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论!

本站内容由网友原创或转载,如果侵犯了您的合法权益,请及时联系处理!© admin@sapbbs.com

快速回复 返回顶部 返回列表