Replace leading spaces with Notepad++ - regex

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 #.

Related

Find and replace using regular expressions - remove double spaces between letters only

Trying to do this in the Atom editor (1.39.1 x64, uBuntu 18.04), though assume this applies to other text editors using regular expressions.
Say we have this text:
This text has some double-spaces. Lets try to remove them.
But not after a full-stop or if three or more spaces.
Which we would like to change to:
This text has some double-spaces. Lets try to remove them.
But not after a full-stop or if three or more spaces.
Using Find with Regex enabled (.*), all occurrences are correctly found using: [a-zA-Z] [a-zA-Z]. But what goes in the Replace row to enforce the logic:
1st letter, single space, 2nd letter?
You can use this
([a-z])\s{2}([a-z])
and replace by $1 $2
Regex Demo
If your editor supports lookarounds you can use
(?<=[a-z])\s{2}(?=[a-z])
Replace by single space character
Regex demo
Note:- don't forget to use i flag for case insensitivity or just change the character class to [a-zA-Z]

How can I replace this data in between certain delimiter with Notepad ++?

I have a list of data in this format
0000000000000000|000|000|00000|000000|CITY|GA|123456|8001234567
I need to replace the last piece of data with the word N/A so there is no phone number in the list.
0000000000000000|000|000|00000|000000|CITY|GA|123456|N/A
Thank you for the assistance, much appreciated.
The simplest and fastest solution for that would be to search for
[^|\r\n]+$
and replacing all with N/A.
Explanation:
[^|\r\n]+ matches one or more characters except | or newlines, and $ makes sure that the match only occurs at the end of a line.
Do a find/replace, with the mode set to "Regular expression".
Find:
(.*)\|[0-9]*
Replace:
\1|N/A
If your phone numbers contain any non-numeric characters (such as periods, hyphens, spaces, etc.), then I would recommend the following adjustment to the regex given by #Bitwise:
(.*)\|(.*)$
Also, in Notepad++, the backreference syntax is not
\1
but rather
$1
which means your replace string will actually be
$1|N/A
You can use
(?!.*\|)(.+)
to mark the end of the line.
In Notepad++ you can use the search and replace (regex) function.

Regular expression find/replace notepad++

I've a huge text file with lines like this:
080012;Bovalino;RC;CAL;0964;89034;B098;9021;http://www.website-most.en/000/000/
And i would like extract only:
080012;***Bovalino***;***RC***;CAL;***0964***;***89034***;B098;9021;http://www.website-most.en/000/000/
And delete all other text.
Can this be done with regular expressions?
You can capture the stuff you want to keep and use a backreference in the replacement string:
Find what: ^\d*;(\w*;\w*);\w*;(\d*;\d*).*
Replace with: \1;\2
And make sure you do not tick the . matches newline option.
With Notepad++ 6 you can also use $1;$2 for the replacement (with the same meaning).
If the different fields may contain all sorts of characters and not just digits and letters, this is probably your best bet:
Find what: ^[^;]*;([^;]*;[^;]*);[^;]*;([^;]*;[^;]*).*

regex in Notepad++ to remove blank lines

I have multiple html files and some of them have some blank lines, I need a regex to remove all blank lines and leave only one blank line.. So it removes anything more than one blank line, and leave those that are just one or none (none like in having text in them).
I need it also to consider lines that are not totally blank, as some lines could have spaces or tabs (characters that doesn't show), so I need it to consider these lines with the regex to be removed as long as it is more than one line..
Search for
^([ \t]*)\r?\n\s+$
and replace with
\1
Explanation:
^ # Start of line
([ \t]*) # Match any number of spaces or tabs, capture them in group 1
\r?\n # Match one linebreak
\s+ # Match any following whitespace
$ # until the last possible end of line.
\1 will then contain the first line of whitespace characters, so when you use that as the replacement string, only the first line of whitespace will be preserved (excluding the linebreak at the end).
This worked for me on notepad++ v6.5.1. UNICODE windows 7
Search for: ^[ \t]*\r\n
Replace with: nothing, leave blank
Search mode: Regular expression.
search for (\r?\n(\t| )*){3,}, replace by \r\n\r\n, check "Regular expression" and ". matches newline".
Tested with Notepad++ 6.2
This will replace the successive blank lines containing white spaces (or not) and replace it with one new line.
Search for
(\s*\r?\n){3,}
replace with
\r\n
You can find it yourself what you need to replace with
\n\n OR \n\r\n or \r\n\r\n etc ... now you can even modify your regular expression ^([ \t]*)\r?\n\s+$ according to your need.
I tested any of the above suggestions, always was either too less or to much deleted. So that either you got no blank line where at least one was beforehand or deleted not enough (whitespaces was left, etc.). Unfortunately I cannot write comments yet. Tested both with 6.1.5 and updated to 6.2 and tested again. depending on how mayn files there are, I would suggest use
Edit->Blank Operations->Trim trailing whitespace
Followed by Ctrl+A and
TextFX -> TextFX Edit -> Delete surplus blank lines
A Macro I tried to record didn't work. Theres even a macro for just remove trailing whitespace (Alt+Shift+S, see Settings | Shortcut Mapper... | Macros). There's a
Edit->Blank Operations->Remove unnecessary EOL and whitespace
but that deletes every EOL and puts everything in a single line.
In notepad++ v8.4.7 there is the option:
Edit > Line Operations > Remove Empty Lines (Containing Blank characters)
or
Edit > Line Operations > Remove Empty Lines
So there is no need to use a regular expressions for this. But this only works for one file at a time.
I looked for ^\r\n and click "Replace All" with nothing (empty) in "Replace with" textbox.

notepad++ regex remove numbers

I have a notepad++ text file with:
have 9456
do 9823781
no 83270
tell 342
and it continues like that.
What is the regex for removing the space and numbers from the file?
You want to replace [0-9 ]+ with empty string with the regex option enabled.
Use the following as your search string:
\d+$
Remember to have the Search Mode set to Regular Expression.
Find space and digits easy as one two three
Search \s\d+
Replace all with: nothing