Solved

RegEx N'th Occurrence using Sublime

Posted on 2014-11-10
3
661 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
Facebook React and SyntheticEvents 6 70
Regular expression help 2 35
Powercli + List all VM's Per SCSILUN 15 90
remove computer from using logon script 17 38
This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
I'd like to talk about something that is near and dear to my heart: build systems. Without them, building software is all about compiling locally, with software versions everywhere. It can be a mess. Today we are going to discuss building a small di…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

820 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