注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

XINBO'S SAP BLOG

你所浪费的今天,是昨天死去的人奢望的明天。你所厌恶的现在,是未来的你回不去的曾经

 
 
 

日志

 
 

ABAP中正则表达式的简单使用  

2010-03-16 10:11:52|  分类: REPORTING&&OTHER |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

REPORT  z_barry_test.

DATA: str TYPE string ,
      result_tab TYPE match_result_tab ,
      wa LIKE LINE OF result_tab.

*找出STRING里面的双字节字符
str = 'abc我啊adfsf们'.
FIND ALL OCCURRENCES OF REGEX '[^\x00-\xff]*' IN str RESULTS result_tab.
LOOP AT result_tab INTO wa.
  WRITE / str+wa-offset(wa-length).
ENDLOOP.

*找出STRING里面的单字节字符
str = 'abc我啊adfsf们'.
FIND ALL OCCURRENCES OF REGEX '[\x00-\xff]*' IN str RESULTS result_tab.
WRITE / '-----'.
LOOP AT result_tab INTO wa.
  WRITE / str+wa-offset(wa-length).
ENDLOOP.

*找出STRING里面的IP地址
str = 'IP1:172.16.32.12  IP2:192.168.1.1 '.
FIND ALL OCCURRENCES OF REGEX '\d+\.\d+\.\d+\.\d+' IN str RESULTS result_tab.
WRITE / '-----'.
LOOP AT result_tab INTO wa.
  WRITE / str+wa-offset(wa-length).
ENDLOOP.

*找出STRING里面的***-********格式的电话号码
str = 'IP1:172.16.32.12  021-12345678 '.
FIND ALL OCCURRENCES OF REGEX '\d{3}-\d{8}|\d{4}-\d{7}' IN str RESULTS result_tab.
WRITE / '-----'.
LOOP AT result_tab INTO wa.
  WRITE / str+wa-offset(wa-length).
ENDLOOP.

*找出STRING里面的15/18位身份证号码
str = 'IP1:172.16.32.12 3722198003041234 '.
FIND ALL OCCURRENCES OF REGEX '\d{15}|\d{18}' IN str RESULTS result_tab.
WRITE / '-----'.
LOOP AT result_tab INTO wa.
  WRITE / str+wa-offset(wa-length).
ENDLOOP.

***使用CLASS的例子:
REPORT  z_barry_test.
 
PARAMETERS: p_input TYPE string DEFAULT 'IP1:172.16.32.12  IP2:192.168.1.1 ' OBLIGATORY.
 
DATA: regex   TYPE REF TO cl_abap_regex,
      matcher TYPE REF TO cl_abap_matcher,
      match   TYPE c .
DATA: result_tab TYPE match_result_tab ,
      wa LIKE LINE OF result_tab.
 
CREATE OBJECT regex
  EXPORTING
    pattern     = '\d+\.\d+\.\d+\.\d+'
    ignore_case = 'X'.
 
TRY.
    CALL METHOD regex->create_matcher
      EXPORTING
        text    = p_input
*       table   =
      RECEIVING
        matcher = matcher .
  CATCH cx_sy_matcher .
ENDTRY.
 
TRY.
    CALL METHOD matcher->match "是否完全匹配
      RECEIVING
        success = match.
  CATCH cx_sy_matcher .
ENDTRY.
 
CALL METHOD matcher->find_all
  RECEIVING
    matches = result_tab.
LOOP AT result_tab INTO wa.
  WRITE / p_input+wa-offset(wa-length).
ENDLOOP.
 
 
 
***SAP给的判断Email地址的例子***
PARAMETERS email TYPE c LENGTH 30 LOWER CASE DEFAULT 'sap@sap.com'.
DATA matcher TYPE REF TO cl_abap_matcher.
matcher = cl_abap_matcher=>create( pattern = '\w+(\.\w+)*@(\w+\.)+(\w{2,4})'
                                   ignore_case = 'X'
                                   text = email ).
IF matcher->match( ) IS INITIAL.
  MESSAGE 'Wrong Format' TYPE 'I'.
ELSE.
  MESSAGE 'Format OK' TYPE 'I'.
ENDIF.
  评论这张
 
阅读(696)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017