Solved

RegEx N'th Occurrence using Sublime

Posted on 2014-11-10
3
631 Views
Last Modified: 2014-11-12
I have already asked a similar question here: Regex

I have this regex working within VI which replaces the 3rd occurrence of ',' with 'Unique,'
s/,.\{-},.\{-}\zs,/Unique,

Open in new window


I can't seem to get it to work within Sublime Text Editor. I have tried the following regex, but it doesn't replace the 3rd comma, it instead captures it and places it into my replacement string.

Sample Data: echo "clientRequests#300#raw,$EPOCH_TIME,$_importExternalEmailData,proj_ott,ottcore
Regex Find: (,.*?,.*?,)
Replace Str: $1Unique

I want to find the 3rd comma so I can append to the name $_importExternalEmailData so the result is $_importExternalEmailDataUnique. Right now it will put 'Unique' after the 3rd comma.
0
Comment
Question by:lconnell
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 40434498
If you just replace the third comma, the result will be:
echo "clientRequests#300#raw,$EPOCH_TIME,$_importExternalEmailDatauniqueproj_ott,ottcore

Open in new window

I think you probably want to keep the third comma, and insert "unique" before it. Am I right? If so, try:

Find:
(,.*?,.*?)(?=,)

Open in new window

And replace with:
$1unique

Open in new window

Result:
echo "clientRequests#300#raw,$EPOCH_TIME,$_importExternalEmailDataunique,proj_ott,ottcore

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 40434500
(?=,) is a positive lookahead, which requires the following character at that point to be a comma, but doesn't capture it as part of the match. If that's too scary, you could also just put the comma back in as part of the replacement, with:

Find:
(,.*?,.*?),

Open in new window

Replace:
$1unique,

Open in new window

0
 

Author Closing Comment

by:lconnell
ID: 40438569
Great, thank you!!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Where do I learn ES6? 3 69
Allow space in this pattern 2 59
Can I exclude specific directory with icacle command? 3 43
Need some help with regex 3 38
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question