Related
Good Morning in my timezone
I want to replace a character that is in the beginning of each line
So i had used the following regular expression to find the text
^\d
And it works fine in finding all the characters
The problem is in the replace with
I want to replace with single quote followed by the same character found above
How can i do it ?
Thanks in advance
You may try this option:
Find:
^(?=\d)
Replace:
' <-- just a single quote
The find pattern uses a positive lookahead which asserts that the first character is a digit, but nothing is ever matched. Then, the replacement is a single quote.
You may use
Find What: ^\d
Replace With: '$0
where $0 is the backreference to the match value.
Another one would be:
Find:
^(\d)
Replace:
'\1
In this example \1 would be 1st captured group.
I would like to change the second forward slash, within each line, to a comma.
I have found various posts and managed to derive a way of doing it from them but it's not doing it how I want.
Initial attempt - I thought I needed to replace between 2 delimiters
1st "Replace 2nd occurrence" - Found this post which seemed easier.
2nd "Replace 2nd occurrence"- Used the regex in here as a base for mine.
What I am doing is;
Find:
^(.*?)\/(.*?)\/
Replace:
$&,
Which results in changing my data from;
042146/OVERNIGHT/HSSC825571,started,14/07/2016,00:00:56,V0700LWHSB
042146/OVERNIGHT/HSSC825571,ended,14/07/2016,00:00:56,
042147/OVERNIGHT/HSSC825571,started,14/07/2016,00:00:58,V0700LWHSB
042147/OVERNIGHT/HSSC825571,ended,14/07/2016,00:00:58,
To;
042146/OVERNIGHT/,HSSC825571,started,14/07/2016,00:00:56,V0700LWHSB
042146/OVERNIGHT/,HSSC825571,ended,14/07/2016,00:00:56,
042147/OVERNIGHT/,HSSC825571,started,14/07/2016,00:00:58,V0700LWHSB
042147/OVERNIGHT/,HSSC825571,ended,14/07/2016,00:00:58,
Is there a way of just replacing the second /?
An example set of my data is;
042146/OVERNIGHT/HSSC825571,started,14/07/2016,00:00:56,V0700LWHSB
042146/OVERNIGHT/HSSC825571,ended,14/07/2016,00:00:56,
042147/OVERNIGHT/HSSC825571,started,14/07/2016,00:00:58,V0700LWHSB
042147/OVERNIGHT/HSSC825571,ended,14/07/2016,00:00:58,
042154/TEMP56/QPADEV000M,started,14/07/2016,00:01:02,V0700LRFIN
042154/TEMP56/QPADEV000M,ended,14/07/2016,00:07:12,
042155/JMALICKA/QPADEV000N,started,14/07/2016,00:01:05,V0700LRFIN
042155/JMALICKA/QPADEV000N,ended,14/07/2016,00:06:53,
042156/DG8SVCPRF/DG8SVC,started,14/07/2016,00:01:15,DATAGATE
042156/DG8SVCPRF/DG8SVC,ended,14/07/2016,00:12:01,
042157/OVERNIGHT/RCPTDISCRP,started,14/07/2016,00:01:42,V0700LBATC
042157/OVERNIGHT/RCPTDISCRP,ended,14/07/2016,00:01:44,
042158/QTCP/QTSMTPCLTP,started,14/07/2016,00:01:53,QSYSWRK
042158/QTCP/QTSMTPCLTP,ended,14/07/2016,01:29:08,
042159/QTCP/QTSMTPCLTP,started,14/07/2016,00:01:53,QSYSWRK
042159/QTCP/QTSMTPCLTP,ended,14/07/2016,00:19:05,
Ctrl+H
Find what: ^([^/]+/[^/]+)/
Replace with: $1,
Replace all
This will replace the second slash of each line by a comma.
You were almost there. You only need to change your replace string with the following:
$1/$2,
How it works
Your regex was: ^(.*?)\/(.*?)\/
In Notepad++'s replace string, the dollar sign is used to refer to groups enclosed by parentheses in the regex.
$1 refers to the first group, (.*?) which is at the beginning of the line, as specified with the ^ character.
$2 refers to the second group, also (.*?), but which follows the first /.
Since you don't want to replace the first slash, you need $1/$2 at the beginning of your replace string. But since what follows the second group is another / (the 2nd one on the line), you need to replace it with the ,. That's why the replace string has to be $1/$2,. Notice that all characters that are not enclosed by ()'s need to be re-written in the replace string. Otherwise, they're just omitted (try replace string $1$2 and you'll see what I mean).
In other editors or programming languages, instead of the $ sign, the \ is sometimes used (sometimes doubled) to refer to parenthetic groups. So you could have for instance \\1/\\2, or \1/\2, as a replace string instead of $1/$2,.
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
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
In the following string,
apache:x:48:48:Apache:/var/www:/sbin/nologin
how could I replace the first colon (and this one only) with a comma so I would get the following string?
apache,x:48:48:Apache:/var/www:/sbin/nologin
Also, the code has to support a file with multiple lines and replace the first comma in each line only.
Use a regular expression:
PS C:\> $s = 'apache:x:48:48:Apache:/var/www:/sbin/nologin'
PS C:\> $s -replace '^(.*?):(.*)','$1,$2'
apache,x:48:48:Apache:/var/www:/sbin/nologin
Regexp breakdown:
^(.*?):: shortest match between the beginning of the string and a colon (i.e. the text before the first colon).
(.*): the remainder of the string (i.e. everything after the first colon).
The parantheses group the subexpressions, so they can be referenced in the replacement string as $1 and $2.
Further explanation:
^ matches the beginning of a string.
.* matches any number of characters (. ⇒ any character, * ⇒ zero or more times).
.*? does the same, but gives the shortest match (?) instead of the longest match. This is called a "non-greedy match".