代码5S
分类:
2020-05-20 14:02:44
原文地址:ABAP中正则表达式的简单使用 作者:qdbarry
在一个论坛上面看到有人在问正则表达式的问题,特举例简单说明一下。
另外,REPLACE也支持REGEX关键字。
最后:只能是ECC6或者更高版本才可以(ABAP supports POSIX regular expressions as of Release 7.00)
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.
注:ECC6下有个专门测试正则表达式的工具程序:DEMO_REGEX_TOY