当前位置:网站首页>现代 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 ).
结论
边栏推荐
- C# TextBlock 上标
- 进程间通信方式
- IP报文头解析
- 卖家寄卖流程梳理
- Why, when you added a unique index or create duplicate data?
- Next -20- 使用自定义样式 (custom style)
- JVM调优-GC基本原理和调优关键分析
- 聊聊与苹果审核员的爱恨情仇
- Projector reached the party benefits 】 【 beginners entry - brightness projection and curtain selection - from entry to the master
- 你一定从未看过如此通俗易懂的YOLO系列(从v1到v5)模型解读
猜你喜欢
DocuWare平台——用于文档管理的内容服务和工作流自动化的平台(上)
(2022杭电多校五)C - Slipper (dijkstra+虚拟结点)
【伸手党福利】投影仪初学者入门——投影亮度及幕布选择——从入门到精通
Byte、Short、Integer、Long内部缓存类的对比与源码分析
Why, when you added a unique index or create duplicate data?
西安纵横资讯×JNPF:适配中国企业特色,全面集成费用管控体系
浅谈一下跨端技术方案
To ensure that the communication mechanism
重构指标之如何监控代码圈复杂度
24、shell编程-流程控制
随机推荐
Roslyn 节点的 Span 和 FullSpan 有什么区别
性能提升400倍丨外汇掉期估值计算优化案例
Manacher(求解最长回文子串)
我说MySQL联合索引遵循最左前缀匹配原则,面试官让我回去等通知
Next -18- 添加代码复制按钮
界面组件DevExpress ASP.NET Core v22.1 - 增强数据导出功能
Sublime Text 好用的插件
What is an artifact library in a DevOps platform?What's the use?
QT笔记——QUuid了解
实战:10 种实现延迟任务的方法,附代码!
365天挑战LeetCode1000题——Day 049 非递增顺序的最小子序列 贪心
如何优雅的消除系统重复代码?
H5 之 文件流转base64下载
爬虫小白笔记(昨天的对于注意解析数据的补充)
弄懂#if #ifdef #if defined
你以为在做的是微服务?不!你做的只是分布式单体!
全球电子产品需求放缓,三星手机越南工厂每周只需要干 3~4 天
Game network UDP + FEC + KCP
使用百度EasyDL实现森林火灾预警识别
多线程编程之优先级翻转问题