当前位置:网站首页>现代 ABAP 编程语言中的正则表达式
现代 ABAP 编程语言中的正则表达式
2022-08-04 15:35:00 【InfoQ】
ABAP 正则表达式的回顾
FIND 'A' IN 'ABCD1234EFG'
MATCH COUNT sy-tabix.
WRITE: sy-tabix.
FIND ALL OCCURRENCES OF PCRE '[A-Z]' IN 'ABCD1234EFG'
MATCH COUNT sy-tabix.
WRITE: sy-tabix.
Greedy or Lazy?
DATA(text) = `"Jack" and "Jill" went up the "hill"`.
FIND ALL OCCURRENCES OF PCRE `"(.*?)"` IN text IGNORING CASE
RESULTS DATA(result_tab).
IF sy-subrc = 0.
LOOP AT result_tab ASSIGNING FIELD-SYMBOL(<result>).
cl_demo_output=>write( substring( val = text off = <result>-offset len = <result>-length ) ).
ENDLOOP.
ENDIF.
cl_demo_output=>display( ).

PCRE Syntax
DATA(text) = `oooababboo`.
FIND PCRE 'a.|[ab]+|b.*' IN text
MATCH OFFSET DATA(moff)
MATCH LENGTH DATA(mlen).
IF sy-subrc = 0.
cl_demo_output=>write( substring( val = text off = moff len = mlen ) ).
ENDIF.
Callouts in PCRE Regular Expressions
REPORT demo_pcre_callout.
CLASS handle_regex DEFINITION.
PUBLIC SECTION.
INTERFACES if_abap_matcher_callout.
ENDCLASS.
CLASS handle_regex IMPLEMENTATION.
METHOD if_abap_matcher_callout~callout.
cl_demo_output=>write( |{ callout_num } { callout_string }| ).
ENDMETHOD.
ENDCLASS.
CLASS demo_pcre DEFINITION.
PUBLIC SECTION.
CLASS-METHODS main.
ENDCLASS.
CLASS demo_pcre IMPLEMENTATION.
METHOD main.
DATA(regex) = cl_abap_regex=>create_pcre(
pattern = `a(?C1)b(?C2)c(?C3)d(?C"D")e(?C"E")` ).
DATA(matcher) = regex->create_matcher( text = `abcde` ).
DATA(handler) = NEW handle_regex( ).
matcher->set_callout( handler ).
matcher->match( ).
cl_demo_output=>display( ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
demo_pcre=>main( ).
PCRE syntax for ABAP SQL and ABAP CDS
CDS View Entity
REPLACE_REGEXPR(PCRE => pcre,
VALUE => arg1,
WITH => arg2,
RESULT_LENGTH => res[,
OCCURRENCE => occ][,
CASE_SENSITIVE => case][,
SINGLE_LINE => bool][,
MULTI_LINE => bool][,
UNGREEDY => bool])
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view entity ZI_regex_test
as select from spfli
{
concat_with_space( cityfrom, cityto, 4 ) as from_City_to,
distance as Distance,
distid as DistanceId,
case
when distid = 'MI' then
replace_regexpr( pcre => '[^<]+',
value => distid,
with => '1.6 KM',
result_length => 6 )
else 'KM'
end as DistanceIdInKM
}
SQL Expressions
SELECT * FROM spfli
WHERE
like_regexpr( pcre = '\bBERLIN\b|\bTOKYO\b', value = cityfrom ) = '1'
INTO @ls_table.
APPEND ls_table TO lt_table.
ENDSELECT.
SELECT
carrid as Airline,
connid as flightNo,
deptime as Departure_time,
cityfrom as Departure,
replace_regexpr( pcre = '\bROME\b', value = cityto , with = 'Neapel' ) as Destination
from spfli where cityfrom = 'TOKYO'
into TABLE @data(lt_replace) .
Xpath and XSD Syntax
FIND REGEX
cl_abap_regex=>create_xpath2( pattern = '[\p{IsBasicLatin}-[a-c]]' )
IN 'abcd' MATCH OFFSET DATA(moff).
DATA(x) = match( val = `abxcd` xpath = `\x` occ = 1 ).
DATA(y) = match( val = `abxcd` pcre = `\x` occ = 1 ).
FIND REGEX cl_abap_regex=>create_xpath2( pattern = '\x' ) IN 'abxcd'.
FIND REGEX cl_abap_regex=>create_pcre( pattern = '\x' ) IN 'abxcd'.
XSD Syntax
DATA(xml) = `<A><B>...<Y><Z>`.
REPLACE ALL OCCURRENCES OF
REGEX cl_abap_regex=>create_xsd( pattern = `\i\c*` )
IN xml WITH `option:$0`.
cl_demo_output=>display( xml ).
结论
边栏推荐
猜你喜欢

Projector reached the party benefits 】 【 beginners entry - brightness projection and curtain selection - from entry to the master

AAAI‘22 推荐系统论文梳理

我在羊毛和二手群里报复性消费

How to monitor code cyclomatic complexity by refactoring indicators

保证通信的机制有哪些

24、shell编程-流程控制

What is the difference between ITSM software and a work order system?

A detailed explanation of what is software deployment

(2022杭电多校五)C - Slipper (dijkstra+虚拟结点)

What is an artifact library in a DevOps platform?What's the use?
随机推荐
To ensure that the communication mechanism
What is the difference between member variable and local variable
Nuget 通过 dotnet 命令行发布
NUS颜水成等发布首篇《深度长尾学习》综述
QT笔记——Q_INVOKABLE了解
Next -20- 使用自定义样式 (custom style)
你一定从未看过如此通俗易懂的YOLO系列(从v1到v5)模型解读
C# 谁改了我的代码
SublimeText 粘贴图片保存到本地
24、shell编程-流程控制
从-99打造Sentinel高可用集群限流中间件
IP报文头解析
"Research Report on the Development of Global Unicorn Enterprises in the First Half of 2022" released - DEMO WORLD World Innovation Summit ended successfully
素士科创板IPO撤单,雷军失去“电动牙刷第一股”
Go 言 Go 语,一文看懂 Go 语言文件操作
手搓一个“七夕限定”,用3D Engine 5分钟实现烟花绽放效果
游戏网络 UDP+FEC+KCP
我说MySQL联合索引遵循最左前缀匹配原则,面试官让我回去等通知
C# SolidWorks二次开发---工程图简单版标注孔信息
MVCC实现过程