abap正则表达式实例
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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 = cl_abap_matcher=>create( pattern = '\w+(\.\w+)*@(\w+\.)+(\w{2,4})'
ignore_case = 'X'
text = email ).
matcher TYPE REF TO cl_abap_matcher,
match TYPE c .
DATA: rab ,
wa LIKE LINE OF result_tab.
CREATE OBJECT regex
IF matcher->match( ) IS INITIAL.
MESSAGE 'Wrong Format' TYPE 'I'.
ELSE.
MESSAGE 'Format OK' TYPE 'I'.
ENDIF.
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).
WRITE / '-----'.
LOOP AT result_tab INTO wa.
WRITE / str+wa-offset(wa-length).
ENDLOOP.
*找出STRING里面的15/18位身份证号码
str = 'IP1:172.16.32.12 3722198003041234 '.
* table =
RECEIVING
matcher = matcher .
CATCH cx_sy_matcher .
ENDTRY.
TRY.
CALL METHOD matcher->match "是否完全匹配
RECEIVING
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).
EXPORTING
pattern = '\d+\.\d+\.\d+\.\d+'
ignore_case = 'X'.
TRY.
CALL METHOD regex->create_matcher
EXPORTING
text = p_input
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 / '-----'.
success = match.
CATCH cx_sy_matcher .
ENDTRY.
CALL METHOD matcher->find_all
RECEIVING
matches = result_tab.
LOOP AT result_tab INTO wa.
REPORT z_barry_test.
DATA: str TYPE string ,
result_tab TYPE match_result_tab ,
wa LIKE LINE OF result_tab.
*找出STRING里面的双字节字符
str = 'abc我啊adfsf们'.
WRITE / p_input+wa-offset(wa-length).
ENDLOOP.
***SAP给的判断Email地址的例子***
PARAMETERS email TYPE c LENGTH 30 LOWER CASE DEFAULT 'sap@'.
DATA matcher TYPE REF TO cl_abap_matcher.
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里面的单字节字符
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.
***使用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 = cl_abap_matcher=>create( pattern = '\w+(\.\w+)*@(\w+\.)+(\w{2,4})'
ignore_case = 'X'
text = email ).
matcher TYPE REF TO cl_abap_matcher,
match TYPE c .
DATA: rab ,
wa LIKE LINE OF result_tab.
CREATE OBJECT regex
IF matcher->match( ) IS INITIAL.
MESSAGE 'Wrong Format' TYPE 'I'.
ELSE.
MESSAGE 'Format OK' TYPE 'I'.
ENDIF.
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).
WRITE / '-----'.
LOOP AT result_tab INTO wa.
WRITE / str+wa-offset(wa-length).
ENDLOOP.
*找出STRING里面的15/18位身份证号码
str = 'IP1:172.16.32.12 3722198003041234 '.
* table =
RECEIVING
matcher = matcher .
CATCH cx_sy_matcher .
ENDTRY.
TRY.
CALL METHOD matcher->match "是否完全匹配
RECEIVING
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).
EXPORTING
pattern = '\d+\.\d+\.\d+\.\d+'
ignore_case = 'X'.
TRY.
CALL METHOD regex->create_matcher
EXPORTING
text = p_input
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 / '-----'.
success = match.
CATCH cx_sy_matcher .
ENDTRY.
CALL METHOD matcher->find_all
RECEIVING
matches = result_tab.
LOOP AT result_tab INTO wa.
REPORT z_barry_test.
DATA: str TYPE string ,
result_tab TYPE match_result_tab ,
wa LIKE LINE OF result_tab.
*找出STRING里面的双字节字符
str = 'abc我啊adfsf们'.
WRITE / p_input+wa-offset(wa-length).
ENDLOOP.
***SAP给的判断Email地址的例子***
PARAMETERS email TYPE c LENGTH 30 LOWER CASE DEFAULT 'sap@'.
DATA matcher TYPE REF TO cl_abap_matcher.
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里面的单字节字符
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.