* *,0或多个字母、数字、下划线
* +,0或多个字母、数字、下划线
* \d,numc
* \w,letter, numc, _
* {2,4}, lenth 2 to 4
* (), it's not necessary in ()
另外,replace也支持regex关键字。
最后:只能是ecc6或者更高版本才可以(abap supports posix regular expressions as of release 7.00)
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.
注:ecc6下有个专门测试正则表达式的工具程序:demo_regex_toy
评论