Codename_DJ
Codename_DJ

Reputation: 563

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

    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. like for string < xxxx,9898,yyyy,87,zzzz,56,aaaa > , i want to get < xxxx,9898,yyyy,87,zzzz >

I am doing regexp based on the pattern ,[0-9], and using this code

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

but string is trimmed from first occurance and looks like

    AVPs_List,Vendor_Specific_Application_Id

Please help.

Upvotes: 2

Views: 2163

Answers (1)

Jerry
Jerry

Reputation: 71538

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]+,.

Upvotes: 3

Related Questions