Regular expression to match only the first occurrence of a comma in each line

What does a regular expression look like for matching only the first instance with a comma and nothing but this comma?

I tried things like ,{1} , and I think this has something to do with non-greedy classifiers, such as ,(.*?) , But I have not been successful.

I am using Notepad ++ to try to convert code from another language to JavaScript. I want to turn the first comma into a colon. It looks like this:

 'TJ', 'Tajikistan' , 'TZ', 'Tanzania' , 'TH', 'Thailand' , 'TL', 'Timor-Leste' , 'TG', 'Togo' , 'TK', 'Tokelau' , 'TO', 'Tongo' , 'TT', 'Trinidad and Tobago' , 

Find what: /,/
Replace with :
0 replaced

Screnshot of Notepad ++

+4
source share
1 answer

What you can do, instead of just replacing the first comma with a colon, you can automatically replace the comma and everything after it with a colon plus everything that was after the comma. (For example, in 'TZ', 'Tanzania' , this approach would replace , 'Tanzania' , with : 'Tanzania' , ) After that, since the rest of the line has already passed the replacement, Notepad ++ does not revise it to see does it contain a comma.

How do you do this using a capture group that allows the replacement string to include part of what matches the regular expression.

In particular, you would replace this ("Find what"):

 ,(.*) 

means "comma ( , ) plus zero or more characters ( .* ) and capture the last ( () )", with this ("Replace with"):

 :$1 

means a colon ( : plus everything that was captured by the first capture group ( $1 ).

+13
source

Source: https://habr.com/ru/post/1492190/


All Articles