简体   繁体   English

正则表达式最后一次出现到tcl中字符串结尾的模式

[英]regexp last occurance of a pattern to end of string in tcl

I have a string like 我有一个像

    AVPs_List,Vendor_Specific_Application_Id,3,AVP.Vendor_ID,0,AVP.Header,AVP.Code

I want to regexp / regsub to get a output like 我想regexp / regsub得到类似的输出

    AVPs_List,Vendor_Specific_Application_Id,3,AVP.Vendor_ID

Actually I want to subtract the part from the last occurance of the pattern " ,[0-9], " to the end of string. 实际上,我想从模式“ ,[0-9] ”的最后一次出现到字符串末尾减去该部分。 like for string < xxxx,9898,yyyy,87,zzzz,56,aaaa > , i want to get < xxxx,9898,yyyy,87,zzzz > 像字符串< xxxx,9898,yyyy,87,zzzz,56,aaaa >,我想得到< xxxx,9898,yyyy,87,zzzz >

I am doing regexp based on the pattern ,[0-9], and using this code 我正在基于模式[0-9]进行正则表达式,并使用此代码

    regsub {(,)([0-9]+)(,).*} $str1 "" $result

but string is trimmed from first occurance and looks like 但是字符串从第一次出现时就被修剪掉了,看起来像

    AVPs_List,Vendor_Specific_Application_Id

Please help. 请帮忙。

You can match instead, like this: 您可以改为匹配,如下所示:

% set str1 "AVPs_List,Vendor_Specific_Application_Id,3,AVP.Vendor_ID,0,AVP.Header,AVP.Code"
AVPs_List,Vendor_Specific_Application_Id,3,AVP.Vendor_ID,0,AVP.Header,AVP.Code
% regexp {(.*),[0-9]+,} $str1 -> result
1
% puts $result
AVPs_List,Vendor_Specific_Application_Id,3,AVP.Vendor_ID

The .* is being greedy and matches as many characters, before it matches the last ,[0-9]+, . .*表示贪婪,并且匹配最后一个,[0-9]+,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM