maxy
maxy

Reputation: 31

Notepad++, replace the first and second comma to ":"

In Notepad++, I'd like to replace only the first and second comma (","), by ":".

Example :

blue,black,red -> blue:black:red (2 first commas replaced)
blue,black,red,yellow -> blue:black:red,yellow  (third comma still here)

Thanks!

Upvotes: 2

Views: 3401

Answers (6)

Avinash Raj
Avinash Raj

Reputation: 174826

Just two capturing groups is enough.

Regex:

^([^,]*),([^,]*),

Replacement string:

$1:$2:

DEMO

Explanation:

  • ^ Asserts that we are at the start.
  • ([^,]*) Captures any character not of , zero or more times and stored it into a group.(ie, group 1)
  • , Matches a literal , symbol.
  • ([^,]*) Captures any character not of , zero or more times and stored it into a group.(ie, group 2)
  • , Matches a literal , symbol.

Upvotes: 1

Erlesand
Erlesand

Reputation: 1535

Something along this line,

^([^,]*),([^,]*),(.*)$ 

And replace with

$1:$2:$3

Or \1:\2:\3

Upvotes: 1

Professor of programming
Professor of programming

Reputation: 3093

Click on the menu item: Search > Replace

In the dialog box that appears, set the following values...

Find what: ^([^,]+),([^,]+),

Replace with: $1:$2:

Search Mode: Regular expression

Upvotes: 0

PurkkaKoodari
PurkkaKoodari

Reputation: 6809

I believe you can do this by replacing this regex:

^([^,]*),([^,]*),(.*)$

With this:

$1:$2:$3

For compatibility with cases where there are less than 2 commas, use these:

^(([^,]*),)?(([^,]*),)?(.*)$

$2:$4:$5

Upvotes: 2

Jongware
Jongware

Reputation: 22478

A no-brainer; virtually "GREP 1-0-1". Not really an effort.

Just find

^([^,]+),([^,]+),

and replace with

\1:\2:

Upvotes: 0

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477533

Well you can try to capture the parts in groups and then replace them as follows:

/^([^,]*),([^,]*),(.*)$/$1:$2:$3

How does it work: each line is matched such that the first part contains all data before the first comma, the second part in between the two commas and the third part all other characters (including commas).

This is simply replaced by joining the groups with colons.

Upvotes: 0

Related Questions