Remove multiple commas at the end of lines using Notepad++ regex replace - regex

I have a text below with data in a CSV file
2,3
4,5
6,7
When I save an open this in notepad++, it has extra commas like
2,3,,,,
4,5,,
6,7,,,,,
like you see, there are variable number of leading commas,
I tried a regex match using:
/,{2,}/
I have selected the regular expressions combo-box from the search mode in ctrl + H Replace box.
For some reason this is not working. What do I need to do to match multiple comma and not get rid of single comma?
Is there a better way to get this done in notepad++?

Regex:
,{2,}$
Replacement string:
empty string
This will replace two or more trailing commas with an empty string. To remove all the trailing commas then use ,+$ regex.

\d+(?:,\d+)?\K.*$
You can use this.Replace by empty string.This will work with data like 2,3,
See demo.
https://regex101.com/r/iS6jF6/9

Related

Regex remove everything else except one number

I have a string consists of multiple lines (output from powershell) like
....
junk line
junk line
MyVaraible=xxxx
junk line
junk line
....
I need to use one regex to get rid of all the junk lines and extract the variable value.
It is super easy if I can loop thru all the lines where I can just do
"MyVaraible=(\d+)" replace with "$1"
But I'm being super restricted by this ancient system where one regex replacement is all I am allow to do.
You may use this regex based replacement.
Search using this regex:
/^\w+=(.*)$|.*(\n|\z)/
Replace using back-reference:
$1
RegEx Demo
RegEx ^\w+=(.*)$|.*(\n|\z) matches a name-value pair separated by = or it matches a full line followed by line-break or end of string.

remove all commas between quotes with a vim regex

I've got a CSV file with lines like:
57,13,"Bob, Bill and Susan",Student,Club,Funded,64,3200^M
I need them to look like
57,13,Bob-Bill-and-Susan,Student,Club,Funded,64,3200
I'm using vim regexes. I've broken it down into 4 steps:
Remove ^M and insert newlines:
:%s:<ctrl-V><ctrl-M>:\r:g`
Replace all with -:
:%s: :\-:g
Remove commas between quotes: Need help here.
Remove quotes:
:%s:\"\([^"]*\)\":\1:g
How do I remove commas between quotes, without removing all commas in the file?
Something like this?
:%s:\("\w\+\),\(\w\+"\):\1 \2:g
My preferred solution to this problem (removing commas inside quoted regions) is to use replacements with an expression instead of trying to get this done in one regex.
To do this you need to prepend you replacement with \= to get the replacement treated as a vim expression. From here you can extract just the parts between quotes and then manipulate the the matched part separately. This requires having two short regexes instead of one complicated one.
:%s/".\{-}"/\=substitute(submatch(0), ',', '' , 'g')/g
So ".\{-}" matches anything in quotes (non greedy) and substitute(submatch(0), ',', '' , 'g') takes what was matched and removes all of the commas and its return value is used as the actual replacement.
The relevant help page is :help sub-replace-special.
As for the other parts of your question. Step 1 is essentially trying to remove all carriage returns since the file format is actually the dos file format. You can remove them with the dos2unix program.
In Step 2 escaping the - in the replacement is unnecessary. So the command is just
:%s/ /-/g
In Step 4, you have an overly complicated regex if all you want to do is remove quotes. Since all you need to do is match quotes and remove them
:%s/"//g
:%s:\("\w*\)\(,\)\(.*"\):\1\3:g
example: "this is , an, example"
\("\w*\) match start of " every letter following qoutes group \1 for back reference
\(,\) capture comma group \2 for back reference
(.*"\) match every other character upto the second qoute ->group 3 for backreference
:\1\3: only include groups without comma, discard group 2 from returned string which is \2
:%s:\("\w*\)\(,\)\(.*"\):\1\3:g removes commas

How to format the content of the file in notepad++ using regular expression?

I have a file abc.txt which has data like this when I opened up in notepad++
10.114.128.196, 10.149.53.72, 40.169.74.47
Is there any way I can make it like this using regular expressions in notepad++?
10.114.128.196,abc
10.149.53.72,abc
40.169.74.47,abc
Search for
(\d{1,3}(\.\d{1,3}){3})[,\s]*
and replace with
$1,abc\n
(\d{1,3}(\.\d{1,3}){3}) matches 1 to 3 digits followed by 3 more such groups starting with a ".". Because of the round brackets around the found pattern is stored in capturing group 1, you can reuse this matched text in the replacement by inserting $1.
[,\s]* matches zero or more commas and whitespace characters.
Global replace ", " with ",abc\n"?
On the field search put: ((\d+\.?){4}(.)( ?))
On the field replace put: $1abc\r\n
The last line will not have a comma , so I think it is ok to have just this one to fix ;)

Replace leading spaces with Notepad++

I'd like to use Notepad++ to replace all leading spaces on a line with a like number of given characters. So for instance, I want to change:
zero
one
two
three
into:
zero
#one
##two
###three
I haven't been successful at getting this working. I did find Regex to replace html whitespace and leading whitespace in notepad++, but wasn't able to get the result I wanted.
Is this possible with Notepad++? I'd rather not have to write code to do this...
As Tim's answer indicates, this can't be done in a single search/replace, however here is how you can accomplish the same task fairly quickly using multiple replacements:
Find: ^( *)[ ]
Replace with: \1#
Now just spam the "Replace All" button until it indicates that there were no matches to replace. This will replace a single space at the beginning of each line on each click, so it will require the same number of clicks as your most-indented line.
Make sure "Regular expression" is selected as the search mode.
You would need variable-length lookbehind assertions to do this in a single regex, and Notepad++ doesn't support these.
For the record, in EditPadPro you can search for (?<=^ *)\s and replace with #.

Notepad++ matchin end of line in regexp

I want to transform this
a
b
b
into this
a
b
b
number of empty lines is variable and can be pretty huge. Empty lines contains spaces. I want to use a regexp like \r\n( *\r\n)+, but notepad++ seems not to like those special characters in regexp, tryed also \\r\\n( *\\r\\n)+
Please note that empty lines may contain spaces, so the correct regexp would be something like \\r\\n( *\\r\\n)+
You can do 'replace all' multiple times on
\r\n\r\n -> \r\n
That's with 'Extended' option selected, not 'Regular expression'.
If the empty line contains spaces, then first replace all lines with only spaces with nothing using regex: ^\s+$ -> ''. Then to the extended replacement above.
Alternatively:
You can also replace all \r\n with some sequence of characters that doesn't exists in the document, e.g. ### then use the following regex replacement : '###(\s*###)+' -> '###' and finally replace back the sequence ('###') with \r\n.