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

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!

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

Something along this line,
^([^,]*),([^,]*),(.*)$
And replace with
$1:$2:$3
Or \1:\2:\3

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.

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.

A no-brainer; virtually "GREP 1-0-1". Not really an effort.
Just find
^([^,]+),([^,]+),
and replace with
\1:\2:

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

Related

Regex syntax \(.*\) suppose to remove the ( ) and all characters between ( ). How it actually works?

I am new to Regex world. I would like to rename the files that have time stamp added on the end of the file name. Basically remove last 25 characters before the extension.
Examples of file names to rename:
IMG523314(2021-12-05-14-51-25_UTC).jpg > IMG523314.jpg
Test run1(2021-08-05-11-32-18_UTC).txt > Test run1.txt
To remove 25 characters before the .extension (2021-12-05-14-51-25_UTC)
or if you like, remove the brackets ( ) which are always there and everything inside the brackets.
After the right bracket is always a dot '. "
Will Regex syntax as shown in the Tittle here, select the above? If yes, I wonder how it actually works?
Many Thanks,
Dan
Yes \(.*\) will select the paranthesis and anything inside of them.
Assuming when you ask how it works you mean why do the symbols work how they do, heres a breakdown:
\( & \): Paranthesis are special characters in regex, they signify groups, so in order to match them properly, you need to escape them with backslashes.
.: Periods are wildcard matcher, meaning they match any single character.
*: Asterisks are a quantifier, meaning match zero to inifite number of the previous matcher.
So to put everything together you have:
Match exactly one opening parathesis
Match an unlimited number of any character
Match exactly one closing bracket
Because of that closing bracket requirement, you put a limit to the infinite matching of the asterisk and therefore only grab the parenthesis and characters inside of them.
Yes, it's possible:
a='IMG523314(2021-12-05-14-51-25_UTC).jpg'
echo "${a/\(*\)/}"
and
b='Test run1(2021-08-05-11-32-18_UTC).txt'
echo "${b/\(*\)/}"
Explanation:
the first item is the variable
the second is the content to be replaced \(*\), that is, anything inside paranthesis
the third is the string we intend to replace the former with (it's empty string in this case)

How to use regex to remove semi colons?

Hello I have this kind of data :
a;b;c;d
1;2;3;4
a;g;h;j
f;g;f;d
a;d;8;d
And I would like to modify to have this :
a;bc;d
1;23;4
a;gh;j
f;gf;d
a;d8;d
Obviously I have a lot of lines but every time the semi colons are in the same position. I tried to select the columns with notepad++ and to to replace the semi colon by nothing but the box is grey...
Do you have a solution ?
Thank you !
Here is an online regex tester that i did the work on, you can just replace your data with the samples.
Regex : (\w+;)((\w+);(\w+))(;\w+)
DEMO
Hold ALT+SHIFT and use the arrow keys to select second semicolon and delete it.
OR
Hold ALT and click and drag the mouse to select a second semicolon and delete it.
OR
Find : ^(...)(.)
Replace with: \1
Ctrl+H
Find what: ^[^;]+;[^;]+\K;
Replace with: LEAVE EMPTY
check Wrap around
check Regular expression
Replace all
Explanation:
^ # beginning of string
[^;]+ # 1 or more non semicolon
; # 1 semi colon
[^;]+ # 1 or more non semicolon
\K # forget all we have seen until this position
; # 1 semi colon
Result for given example:
a;bc;d
1;23;4
a;gh;j
f;gf;d
a;d8;d
If the possible values of the data are lowercase characters a-z or a digit you could also capture the first 3 characters using a capturing group and a character class [a-z0-9] and after that match a semicolon. If there can be more than 1 character you could use a quantifier + for the character class like [a-z0-9]+
Then replace with the first capturing group.
Find what
^([a-z0-9];[a-z0-9]);
Replace with
$1
Regex demo
Or Using \K you could find ^[a-z0-9];[a-z0-9]\K; and leave Replace with empty.

How to replace specific character one time

I want to replace character - using regular expression in my text so it would work like this:
Original text: abcd-efg-hijk-lmno
Text after replacing: abcd-efg-hijk/lmno
As you can see I want to replace character - starting from the end just one time with character /.
Thanks in advance for any tips
Find what: -([^-]*)$
Replace with: /$1
Search Mode: Regular Expression
Explanation:
- : a dash
([^-]*$) : text with no dash,
zero or more times,
to the end of the line,
put in the $1 variable
/$1 : literal "/", contents of $1
Good resource: http://www.grymoire.com/Unix/Regular.html
To replace characters in Notepad++, you can open the Replace window using Ctrl+H, or under the "Search" menu. Once open, enter the following regular expression:
(.{4}-.{3}-.{4})(-)(.{4})
This will find:
a group of four characters (the "." being any character, the "{4}" being the quantity),
a dash,
a group of three characters,
another dash,
a group of four characters,
again another dash,
then a group of four characters.
The parentheses group this search into captured groups, which we will use for the replacement part. See https://www.regular-expressions.info/brackets.html for more info.
If you want to restrict the search to lowercase letters as in your example, you would replace the "." with "[a-z]", or for upper and lower "[a-z,A-Z]".
Now for the replacement. The groups from earlier are referenced by the dollar sign then the number, e.g. $1 would be the first. So we will replace the characters found with the first group ($1), disregard the second group containing the dash and insert the "/" instead, then include the third group ($3):
$1/$3
The settings in the replace window need to have "Regular expression" and "Wrap around" checked, and ". matches newline" unchecked.
You can then click Replace all to replace all occurrences, or go through using Replace individually.
Since the beginning and end of line characters are not included, you can find multiple occurrences of this pattern on a single line.
Note: This answer follows the same procedure as Toto's, however uses a different regular expression.
Ctrl+H
Find what: ^(.+)-([^-]+)$
Replace with: $1/$2
check Wrap around
check Regular expression
DO NOT CHECK . matches newline
Replace all
Explanation:
^ : begining of line
(.+) : 1 or more any character, catch in group 1
- : a dash
([^-]+) : 1 or more any character but dash, catch in group 2
$ : end of line

Regex replace one value between comma separated values

I'm having a bunch of comma separated CSV files.
I would like to replace exact one value which is between the third and fourth comma. I would love to do this with Notepad++ 'Find in Files' and Replace functionality which could use RegEx.
Each line in the files look like this:
03/11/2016,07:44:09,327575757,1,5434543,...
The value I would like to replace in each line is always the number 1 to another one.
It can't be a simple regex for e.g. ,1, as this could be somewhere else in the line, so it must be the one after the third and before the fourth comma...
Could anyone help me with the RegEx?
Thanks in advance!
Two more rows as example:
01/25/2016,15:22:55,276575950,1,103116561,10.111.0.111,ngd.itemversions,0.401,0.058,W10,0.052,143783065,,...
01/25/2016,15:23:07,276581704,1,126731239,10.111.0.111,ll.browse,7.133,1.589,W272,3.191,113273232,,...
You can use
^(?:[^,\n]*,){2}[^,\n]*\K,1,
Replace with any value you need.
The pattern explanation:
^ - start of a line
(?:[^,\n]*,){2} - 2 sequences of
[^,\n]* - zero or more characters other than , and \n (matched with the negated character class [^,\n]) followed with
, - a literal comma
[^,\n]* - zero or more characters other than , and \n
\K - an operator that forces the regex engine to discard the whole text matched so far with the regex pattern
,1, - what we get in the match.
Note that \n inside the negated character classes will prevent overflowing to the next lines in the document.
You can replace value between third and fourth comma using following regex.
Regex: ([^,]+,[^,]+,[^,]+),([^,]+)
Replacement to do: Replace with \1,value. I used XX for demo.
Regex101 Demo
Notepad++ Demo

Eclipse, regular expression search and replace

In eclipse, is it possible to use the matched search string as part of the replace string when performing a regular expression search and replace?
Basically, I want to replace all occurrences of
variableName.someMethod()
with:
((TypeName)variableName.someMethod())
Where variableName can be any variable name at all.
In sed I could use something like:
s/[a-zA-Z]+\.someMethod\(\)/((TypeName)&)/g
That is, & represents the matched search string. Is there something similar in Eclipse?
Thanks!
Yes, ( ) captures a group. You can use it again with $i where i is the i'th capture group.
So:
search: (\w+\.someMethod\(\))
replace: ((TypeName)$1)
Hint: Ctrl + Space in the textboxes gives you all kinds of suggestions for regular expression writing.
Using ...
search = (^.*import )(.*)(\(.*\):)
replace = $1$2
...replaces ...
from checks import checklist(_list):
...with...
from checks import checklist
Blocks in regex are delineated by parenthesis (which are not preceded by a "\")
(^.*import ) finds "from checks import " and loads it to $1 (eclipse starts counting at 1)
(.*) find the next "everything" until the next encountered "(" and loads it to $2. $2 stops at the "(" because of the next part (see next line below)
(\(.*\):) says "at the first encountered "(" after starting block $2...stop block $2 and start $3. $3 gets loaded with the "('any text'):" or, in the example, the "(_list):"
Then in the replace, just put the $1$2 to replace all three blocks with just the first two.
NomeN has answered correctly, but this answer wouldn't be of much use for beginners like me because we will have another problem to solve and we wouldn't know how to use RegEx in there. So I am adding a bit of explanation to this. The answer is
search: (\w+\\.someMethod\\(\\))
replace: ((TypeName)$1)
Here:
In search:
First and last (, ) depicts a group in regex
\w depicts words (alphanumeric + underscore)
+ depicts one or more (ie one or more of alphanumeric + underscore)
. is a special character which depicts any character (ie .+ means
one or more of any character). Because this is a special character
to depict a . we should give an escape character with it, ie \.
someMethod is given as it is to be searched.
The two parenthesis (, ) are given along with escape character
because they are special character which are used to depict a group
(we will discuss about group in next point)
In replace:
It is given ((TypeName)$1), here $1 depicts the
group. That is all the characters that are enclosed within the first
and last parenthesis (, ) in the search field
Also make sure you have checked the 'Regular expression' option in
find an replace box
At least at STS (SpringSource Tool Suite) groups are numbered starting form 0, so replace string will be
replace: ((TypeName)$0)
For someone who needs an explanation and an example of how to use a regxp in Eclipse. Here is my example illustrating the problem.
I want to rename
/download.mp4^lecture_id=271
to
/271.mp4
And there can be multiple of these.
Here is how it should be done.
Then hit find/replace button