*&---------------------------------------------------------------------*
*& Report YN_REGEX_DEMO_02
*&---------------------------------------------------------------------*
*& Author : NathanSun
*& Program Type : DEMO
*& Description : ABAP 正则表达式(第二部分)
*&---------------------------------------------------------------------*
REPORT YN_REGEX_DEMO_02.
DATA: MATCHER TYPE REF TO CL_ABAP_MATCHER,
MATCH TYPE C LENGTH 1.
DATA: MATCHER_A TYPE REF TO CL_ABAP_MATCHER,
MATCH_A TYPE C LENGTH 1.
DATA: MATCHER_B TYPE REF TO CL_ABAP_MATCHER,
MATCH_B TYPE C LENGTH 1.
DATA: MATCHER_C TYPE REF TO CL_ABAP_MATCHER,
MATCH_C TYPE C LENGTH 1.
DATA: MATCHER_D TYPE REF TO CL_ABAP_MATCHER,
MATCH_D TYPE C LENGTH 1.
DATA: MATCHER_E TYPE REF TO CL_ABAP_MATCHER,
MATCH_E TYPE C LENGTH 1.
DATA: MATCHER_F TYPE REF TO CL_ABAP_MATCHER,
MATCH_F TYPE C LENGTH 1.
DATA: MATCHER_G TYPE REF TO CL_ABAP_MATCHER,
MATCH_G TYPE C LENGTH 1.
DATA: MATCHER_H TYPE REF TO CL_ABAP_MATCHER,
MATCH_H TYPE C LENGTH 1.
DATA: MATCHER_I TYPE REF TO CL_ABAP_MATCHER,
MATCH_I TYPE C LENGTH 1.
DATA: MATCHER_J TYPE REF TO CL_ABAP_MATCHER,
MATCH_J TYPE C LENGTH 1.
DATA: MATCHER_K TYPE REF TO CL_ABAP_MATCHER,
MATCH_K TYPE C LENGTH 1.
DATA: MATCHER_L TYPE REF TO CL_ABAP_MATCHER,
MATCH_L TYPE C LENGTH 1.
*使用方括号 [ ] 包含一系列字符,能够匹配其中任意一个字符。用 [^ ] 包含一系列字符,则能够匹配其中字符之外的任意一个字符。同样的道理,虽然可以匹配其中任意一个,但是只能是一个,不是多个。
*匹配 "a" 或 "b" 或 "c" 中任意一个字符
MATCHER = CL_ABAP_MATCHER=>CREATE( PATTERN = '[abc]'
TEXT = 'a' ).
MATCH = MATCHER->MATCH( ).
* 匹配 "a","b","c" 之外的任意一个字符
MATCHER_A = CL_ABAP_MATCHER=>CREATE( PATTERN = '[^abc]'
TEXT = 'd' ).
MATCH_A = MATCHER_A->MATCH( ).
*匹配 "a"~"g" 之间的任意一个字母
MATCHER_B = CL_ABAP_MATCHER=>CREATE( PATTERN = '[a-g]h'
TEXT = 'bh' ).
MATCH_B = MATCHER_B->MATCH( ).
*匹配 "a"~"d","h"~"n" 之外的任意一个字符
MATCHER_C = CL_ABAP_MATCHER=>CREATE( PATTERN = '[^a-dh-n]'
TEXT = 'e' ).
MATCH_C = MATCHER_C->MATCH( ).
*---------------------------------------------------------------------------------------------------
*---------------------------------------------------------------------------------------------------
*修饰匹配次数的特殊符号
*表达式重复3次
MATCHER_D = CL_ABAP_MATCHER=>CREATE( PATTERN = '[abcd]{3}'
TEXT = 'bca' ).
MATCH_D = MATCHER_D->MATCH( ).
*表达式至少重复2次,最多重复6次(可以设置单边界)
MATCHER_E = CL_ABAP_MATCHER=>CREATE( PATTERN = '.{2,6}'
TEXT = '23rg' ).
MATCH_E = MATCHER_E->MATCH( ).
*?代表匹配表达式0次或者1次,相当于 {0,1}
MATCHER_F = CL_ABAP_MATCHER=>CREATE( PATTERN = '[cd]?'
TEXT = '' ).
MATCH_F = MATCHER_F->MATCH( ).
*+代表表达式至少出现1次,相当于 {1,}
MATCHER_G = CL_ABAP_MATCHER=>CREATE( PATTERN = 'a+b'
TEXT = 'aab' ).
MATCH_G = MATCHER_G->MATCH( ).
*'*'表达式不出现或出现任意次,相当于 {0,}
MATCHER_H = CL_ABAP_MATCHER=>CREATE( PATTERN = 'a*b'
TEXT = 'aaaaaaaaab' ).
MATCH_H = MATCHER_H->MATCH( ).
*---------------------------------------------------------------------------------------------------
*---------------------------------------------------------------------------------------------------
*其他一些代表抽象意义的特殊符号
* ^ 代表与字符串开始的地方匹配,不匹配任何字符
MATCHER_I = CL_ABAP_MATCHER=>CREATE( PATTERN = '(^abc)(.{0,})'
TEXT = 'abc123def' ).
MATCH_I = MATCHER_I->MATCH( ).
* $ 代表与字符串结束的地方匹配,不匹配任何字符
MATCHER_J = CL_ABAP_MATCHER=>CREATE( PATTERN = '(.{0,})(def$)'
TEXT = 'abc123def' ).
MATCH_J = MATCHER_J->MATCH( ).
* | 左右两边表达式之间 "或" 关系,匹配左边或者右边
MATCHER_K = CL_ABAP_MATCHER=>CREATE( PATTERN = '(^abc)(.{0,})|(.{0,})(def$)'
TEXT = 'abc12212' ).
MATCH_K = MATCHER_K->MATCH( ).
*---------------------------------------------------------------------------------------------------
*---------------------------------------------------------------------------------------------------
*组合表达式示例:
MATCHER_L = CL_ABAP_MATCHER=>CREATE( PATTERN = '(.{1,3})|(.{5,})'
TEXT = 'bcade' ).
MATCH_L = MATCHER_L->MATCH( ).
WRITE:/ 'ABAP 正则表达式:' ,
/ '多种字符匹配' ,
/ MATCH ,
/ MATCH_A ,
/ MATCH_B ,
/ MATCH_C ,
/ '修饰匹配次数的特殊符号:',
/ MATCH_D ,
/ MATCH_E ,
/ MATCH_F ,
/ MATCH_G ,
/ MATCH_H ,
/ '其他一些代表抽象意义的特殊符号:',
/ MATCH_I ,
/ MATCH_J ,
/ MATCH_K ,
/ '组合表达式示例:',
/ MATCH_L .
ABAP 正则表达式(第二部分)
最新推荐文章于 2022-09-23 16:03:31 发布