Solved

RegEx N'th Occurrence using Sublime

Posted on 2014-11-10
3
707 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
How many times a day do you open, acknowledge, or close an IT incident? What’s your process? Do you have a process depending on the incident, systems involved, and other factors? New Relic Alerts gives you options for how you interact with notifica…
The viewer will learn how to count occurrences of each item in an array.
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 …

732 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