登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

原来为了你

我的人生就是要一次次的超越

 
 
 

日志

 
 
 
 

[转]正则表达式  

2011-10-19 16:41:44|  分类: ABAP基础知识 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

* *,0或多个字母、数字、下划线
* +,0或多个字母、数字、下划线
* \d,numc
* \w,letter, numc, _
* {2,4}, lenth 2 to 4
* (), it's not necessary in ()

另外,replace也支持regex关键字。
最后:只能是ecc6或者更高版本才可以(abap supports posix regular expressions as of release 7.00)

datastr 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.

*找出STRING里面的单字节字符
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.

***使用CLASS的例子:
report  z_barry_test.

parametersp_input type string default 'IP1:172.16.32.12  IP2:192.168.1.1 ' obligatory.

dataregex   type ref to cl_abap_regex,
      matcher 
type ref to cl_abap_matcher,
      match   
type c .
dataresult_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 default 'sap@sap.com'.
data matcher type ref to cl_abap_matcher.
matcher 
cl_abap_matcher=>createpattern '\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.


注:ecc6下有个专门测试正则表达式的工具程序:demo_regex_toy

  评论这张
 
阅读(489)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018