IF cl_abap_matcher=>matches(
pattern = '^(-?[1-9]\d*(\.\d*[1-9])?)|(-?0\.\d*[1-9])$'
text = '1.29' ) = abap_true.
WRITE '数字'.
ELSE.
WRITE '非数字'.
ENDIF.
*DATA: str1 TYPE char20 VALUE '01234'.
*DATA: str2 TYPE char20 VALUE '0123456789'.
*IF str1 CO str2 .
* WRITE:/ '涉及关键字:CO,使用公式 str1 CO str2 。表示 str1 中 的每个字符 在 str2 中都能找到'.
*ENDIF.
*
*
*PARAMETERS field TYPE char10.
*IF contains( val = field regex = '(\d)+$').
* WRITE / '匹配成功'.
*ELSE.
* WRITE / '匹配失败'.
*ENDIF.
*
*PARAMETERS field1 TYPE char10.
*IF contains( val = field1 regex = '(\C)+$').
* WRITE / '匹配成功'.
*ELSE.
* WRITE / '匹配失败'.
*ENDIF.
*
*DATA: lv_para TYPE string.
*lv_para = ' You &are like& kite &flying& in a &hurricane&'.
*REPLACE ALL OCCURRENCES OF REGEX '&[^&]+&' IN lv_para WITH ''.
*WRITE lv_para.
*
*
*DATA lv_str1 TYPE string VALUE 'hello , . world'.
*DATA lv_str2 TYPE string VALUE ','.
*
*IF lv_str1 CS lv_str2.
* WRITE:/ 'str1 中 包含字符串str2'.
*ENDIF.
*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.
*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.
*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 .
*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.
***SAP给的判断Email地址的例子***
*PARAMETERS p_str TYPE c LENGTH 30 .
*DATA matcher TYPE REF TO cl_abap_matcher.
*matcher = cl_abap_matcher=>create( pattern = '\w+[,]+[.]+[(]+[)]+'
* ignore_case = 'X'
* text = p_str ).
*IF matcher->match( ) IS INITIAL.
* MESSAGE 'Wrong Format' TYPE 'I'.
*ELSE.
* MESSAGE 'Format OK' TYPE 'I'.
*ENDIF.
TYPES: BEGIN OF ty_c,
zm_zd TYPE char2,
END OF ty_c.
DATA: lt_ty TYPE TABLE OF ty_c.
DATA: ls_ty LIKE LINE OF lt_ty.
DATA matcher TYPE REF TO cl_abap_matcher.
DATA: lv_a TYPE c LENGTH 10 VALUE '@你好,.()看',
lv_b TYPE c,
lv_c TYPE c,
lv_d TYPE c.
DATA: lv_num TYPE i,
lv_ks TYPE i,
lv_bl TYPE i.
lv_ks = 1.
lv_bl = 0.
lv_num = strlen( lv_a ).
WHILE lv_num >= lv_ks .
ls_ty-zm_zd = lv_a+lv_bl(1).
APPEND ls_ty TO lt_ty.
CLEAR ls_ty.
lv_ks = lv_ks + 1.
lv_bl = lv_bl + 1.
ENDWHILE.
LOOP AT lt_ty INTO ls_ty.
WRITE:/ ls_ty-zm_zd.
ENDLOOP.
LOOP AT lt_ty INTO ls_ty.
matcher = cl_abap_matcher=>create( pattern = '\w'
ignore_case = 'X'
text = ls_ty-zm_zd ).
IF matcher->match( ) IS INITIAL.
IF ',.()' CA ls_ty-zm_zd .
MESSAGE 'Format OK' TYPE 'I'.
ELSE.
lv_b = 'X'.
MESSAGE 'Wrong Format' TYPE 'I'.
EXIT.
ENDIF.
ELSE.
MESSAGE 'Format OK' TYPE 'I'.
ENDIF.
ENDLOOP.
IF lv_b IS NOT INITIAL.
ENDIF.