ABAP正则表达式

 

 http://help.sap.com/saphelp_erp2005/helpdata/en/42/9d6ceabb211d73e10000000a1553f6/content.htm

Expressions in Routines Locate the document in its SAP Library structure

RegularUse

You can use regular expressions in routines.

A regular expression (abbreviation: RegExp or Regex) is a pattern of literal and special characters which describes a set of character strings. In ABAP, you can use regular expressions in the FIND and REPLACE statements, and in classes CL_ABAP_REGEX and CL_ABAP_MATCHER. For more information, see the ABAP key word documentation in the ABAP Editor. This documentation describes the syntax of regular expressions and you can test regular expressions in the ABAP Editor.

Example

This section provides sample code to illustrate how you can use regular expressions in routines.

For Inserting ‘Thousand’ Separators:

This graphic is explained in the accompanying text

* test 1: insert thous. sep
 
l_input = '12345678'.
 l_regex = '([0-9])(?=([0-9]{3})+(?![0-9]))'.
l_new = '$1,'.

REPLACE
* FIRST OCCURRENCE OF
  
ALL OCCURRENCES OF
  REGEX l_regex
  IN l_input WITH l_new
* REPLACEMENT OFFSET off
* REPLACEMENT LENGTH len
* REPLACEMENT COUNT cnt
.

For Converting the US Date Format into the German Date Format:

This graphic is explained in the accompanying text

* Test 2: Date USA->Dt
 
l_input = '6/30/2005'.
 l_regex = '([01]?[0-9])/([0-3]?[0-9])/'.
 l_new = '$2.$1.'.

REPLACE
* FIRST OCCURRENCE OF |
  
ALL OCCURRENCES OF
  REGEX l_regex
  IN l_input WITH l_new
* REPLACEMENT OFFSET off
* REPLACEMENT LENGTH len
* REPLACEMENT COUNT cnt
.

For Converting an External Format into the Internal Format:

This graphic is explained in the accompanying text

* convert data external to internal

DATA: matcher TYPE REF TO cl_abap_matcher,
      submatch1   TYPE string,
      submatch2 type string,
      match type c.

l_input = '6/30/2005'.

clear l_new.

l_regex = '([01]?)([0-9])/([0-3]?)([0-9])/([0-9]{4})'.

matcher = cl_abap_matcher=>create( pattern     = l_regex
                                   text        = l_input ).

match = matcher->match( ).

TRY.
CALL METHOD MATCHER->GET_SUBMATCH
  EXPORTING
    INDEX    = 1
  RECEIVING
    SUBMATCH = submatch1.
    .
 CATCH CX_SY_MATCHER .
ENDTRY.

TRY.
CALL METHOD MATCHER->GET_SUBMATCH
  EXPORTING
    INDEX    = 3
  RECEIVING
    SUBMATCH = submatch2.
    .
 CATCH CX_SY_MATCHER .
ENDTRY.


if submatch1 is initial.
  if submatch2 is initial.
    l_new = '$5/0$4/0$2'.
  else.
     l_new = '$5$3$4/0$2'.
  endif.
else.
  if submatch2 is initial.
    l_new = '$5/0$4$1$2'.
  else.
     l_new = '$5$3$4$1$2'.
  endif.
endif.

REPLACE
* FIRST OCCURRENCE OF |
  
ALL OCCURRENCES OF
  REGEX l_regex
  IN l_input WITH l_new
* REPLACEMENT OFFSET off
* REPLACEMENT LENGTH len
* REPLACEMENT COUNT cnt
.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值