Original post address: http://blog.chinaunix.net/u1/40527/showart.php? Id = 1336138
In a forum, we can see someone asking questions about regular expressions. Here is a simple example.
In addition, replace also supports the RegEx keyword.
Finally, only ecc6 or a later version is supported (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.
* Double-byte characters in string
STR = 'abc, me, 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.
* Find the single-byte characters in the string
STR = 'abc, me, 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.
* Find the IP address in string.
STR = 'ip1: 172.16.32.12 ip2: 192.168.1.1 '.
Find all occurrences of RegEx '/d +/./d +' in STR results result_tab.
Write /'-----'.
Loop at result_tab into WA.
Write/STR + wa-offset (wa-length ).
Endloop.
* Find the phone number in the ***-********* format in the 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.
* Find the 15/18-digit ID number in the string.
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.
* ** Example of using 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 +'
Ignore_case = 'x'. Try.
Call method RegEx-> create_matcher
Exporting
TEXT = p_input
* Table =
Processing ing
Matcher = matcher.
Catch cx_sy_matcher.
Endtry. Try.
Call method matcher-> match "exact match
Processing ing
Success = match.
Catch cx_sy_matcher.
Endtry. Call method matcher-> find_all
Processing ing
Matches = result_tab.
Loop at result_tab into WA.
Write/p_input + wa-offset (wa-length ).
Endloop. *** example of an email address identified by SAP *** 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 '.
Else.
Message 'format OK 'Type 'I '.
Endif.
Note: There is a tool program under ecc6 dedicated to testing Regular Expressions: demo_regex_toy