Solved

RegEx N'th Occurrence using Sublime

Posted on 2014-11-10
3
563 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
Comment Utility
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
Comment Utility
(?=,) 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
Comment Utility
Great, thank you!!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now