Notepad++ newline in regex - find

Suppose you have this file:
x
a
b
c
x
x
a
b
c
x
x
and you want to find the sequence abc (and select the whole 3 lines) with Notepad++ . How to express the newline in regex, please?

Notepad++ can do that comfortably, you don't even need regexes
In the find dialogue box look in the bottom left and switch your search mode to Extended which allows \n etc.
As odds on you're working on a file in windows format you'll be looking for \r\n (carriage return, newline)
a\r\nb\r\nc
Will find the pattern over three lines

Update 18th June 2012
With the new Notepad++ v6, you can indeed search for newlines with regexes. So you can just use
a\r\nb\r\nc
even with regular expressions to accomplish what you want. Note \r\n is Windows encoding of line-breaks. In Unix files, its just \n.
Unfortunately, you can't do that in Notepad++ when using regex search. Notepad++ is based on the Scintilla editor component, which doesn't handle newlines in regex.
You can use extended search for newline searching, but I don't think that will help you search for 3 lines.
More info here.
Update: Robb and StartClass0830 were right about extended search. It does work, but not when using regular expressions search.

^a\x0D\x0Ab\x0D\x0Ac
This will work \x0D is newline and \x0A is carriage return. Assumption is that each line in your file ends with ascii 10 and 13.

I found a workaround for this.
Simply, in Extended mode replace all \r\n to a string that didn't exist in the rest of the document eg. ,,,newline,,, (watch out for special regexp chars like $, &, and *).
Then switch to Regexp mode, do some replacements (now newline is ,,,newline,,,).
Next, switch to Extended mode again and replace all ,,,newline,,, to \r\n.

For Notepad 6 and beyond, do this as a regular expression:
Select Search Mode > Regular expression (w/o . matches newline)
And in the Find what Textbox : a[\r\n]b[\r\n]+c[\r\n]
or if you are looking at the (Windows or Unix) file to see its line breaks as \r\n or \n then you may find it easier to use Extended Mode:
Select Search Mode > Extended (\n, \r, \t, \0, \x...)
And in the Find what Textbox for Windows: a\r\nb\r\nc\r\n
Or in the Find what Textbox for Unix: a\nb\nc\n
Wasn't clear if the OP intent is to select the trailing line return (after the 'c') as well, as would be necessary to remove the lines.
To not select the trailing line return, as appropriate for replacing with a non-empty string, simply remove the final line return from the matching statement.
Note that if there should be a match on the last line of the string, without a matching trailing line return, the match fails.

a\r\nb\r\nc works for me, but not ^a\x0D\x0Ab\x0D\x0Ac
Hmm, too bad that newline is not working with regular expressions. Now I have to go back to Textpad again. :(

Select Search Mode Which is
Extended (\n, \r, \t, \0, \x...)
\n is new line and such
This is Manuel

Find: "(^a.$)\r\n(b.)\r\n^(c.*)$" - pickup 3 whole lines, only storing data
Replace with: "\1\2\3" - Put down (replay) data
Works fine in Regex with Notepad++ v7.9.5
Place holders: ^ Start and $ End of line can be inside or out of ()store as shown, though clearly not necessary in given example. Note "[^x]" is different - here "^" is "NOT".
Advantage of storing and replay allows much more complicated pattern match without having to type in again what you want to end up with, and even change of replay: "\2\3\1" for "bca"

I have run accross this little issue when the document is windows CR/LF
If you click the box for . to match newlines you need .. to match CR/LF so if you have
<blah><blah>",
"<more><blah>
you need to use ",.." to match some string comma cr/lf another string

In Notepad++ you can also try highlighting the desired part of the text and then pressing CTRL+J.
That would justify the text and thus removing all line endings.

Related

How to convert embedded CRLF codes to their REAL newlines in Vscode?

I searched everywhere for this, the problem is that the search criteria is very similar to other questions.
The issue I have is that file (script actually) is embedded in another file. So when I open the parent file I can see the script as massive string with several \n and \r\n codes. I need a way to convert these codes to what they should be so that it formats the code correctly then I can read said code and work on it.
Quick snippet:
\n\n\n\n\nlocal scriptingFunctions\n\n\n\n\nlocal measuringCircles = {}\r\nlocal isCurrentlyCheckingCoherency
Should covert to:
local scriptingFunctions
local measuringCircles = {}
local isCurrentlyCheckingCoherency
perform a Regex Find-Replace
Find: (\\r)?\\n
Replace: \n
If you don't need to reconvert from newlines to \n after you're done working on the code, you can accomplish the trick by simply pressing ctrl-f and substituting every occurrence of \n with a new line (you can type enter in the replace box by pressing ctrl-enter or shift-enter).
See an example ctrl-f to do this:
If after you're done working on the code you need to reconvert to \n, you can add an invisible char to the replace string (typing it like ctrl-enter invisibleChar), and after you're done you can re-replace it with \n.
There's plenty of invisible chars, but I'd personally suggest [U+200b] (you can copy it from here); another good one is [U+2800] (⠀), as it renders as a normal whitespace, and thus is noticeable.
A thing to notice is that recent versions of vscode will show a highlight around invisible chars, but you can easily disable it by clicking on Adjust settings and then selecting Exclude from being highlighted.
If you need to reenable highlighting in the future, you'll have to look for "editor.unicodeHighlight.allowedCharacters" in the settings.

sublime text / ms word delete misc line breaks in code

I have a csv file that has random line breaks throughout the file. (probably load errors when the file was created where the loader somehow managed to put a carriage return into the field)
How do I go in and remove all carriage returns / line breaks where the last character is not "
I have word and sublime text available for text editors
I have tried ^p with a letter infront and find and replace, but that doesnt seem to work for some of the lines for some reason
Example
"3203","Shelving Units
",".033"
instead of
"3203","Shelving Units",".033"
and
"3206","Broom
","1.00"
instead of
"3206","Broom","1.00"
Menu > Find > Replace... or Ctrl+H
Select "Regular Expression" (probably a .* icon in the bottom left, depending on your theme).
Use \n to select newlines (LF) or \r\n (CRLF).
As #GerardRoche said you can use search and replace in Sublime Text. Open it via ctrl+h and press alt+r to toggle regex to enable it. (You may want to create a backup of your file before doing such changes.)
Search for (?<=[^"\n])\n+ and replace it with nothing, press Replace All or ctrl+alt+enter to replace it.
The regex just mean: search for alt least one (+) newlines (\n), that are preceded by something different than a quotation mark or a newline (?<=[^"\n]).
You don't need to worry about carriage returns, because ST only uses them when reading and writing the file and not in the editor.

Removing lines with specific words

I have a text file with multiple lines such as:
amanda: foo
robert: bla
amanda: bar
peter: da
I'd like to remove all lines with amanda. I use ctrl-s and kill each line individually. Is possible to remove all lines at once?
M-x delete-matching-lines. It's possible to use regular expression.
One way is to use query-replace-regexp with a regular expression of ^.*amanda.*$ to an empty string.
Although #Marcos's answer is idiomatic one (you should use it), let's explore other variants. Let's say you have a text buffer and you want to delete a line containing li in it:
Vladimir
Ilich
Ulyanov
Remember, that ^ matches beginning of line and $ end of line in regex. $ doesn't touch the newline character after the line, so replace with regex ^.*li.*$ will produce an empty line, as per #ataylor's answer:
Vladimir
Ulyanov
For some reason it's impossible to match before ^ and after $ in regex, therefore \s-^.*li.*$ nor ^.*li.*$\s- won't work. Note, \s- matches any whitespace character, (i.e. space, tab, newline and carriage return), so intuitively the regexes should've deleted the newline too, as newline is the only possible whitespace character before ^ or after $. To match exactly newline, you should enter it verbatim, C-q C-j by default. Emacs frequently denotes the newline in separate font color as ^J, it's not a sequence of ^ and J, but a single character, please pay attention.
Therefore to delete a line containing li, you could run command query-replace-regexp on string ^.*li.*^J, where ^J is newline:
Vladimir
Ulyanov

How to search for carriage return in eclipse

If I have the following text in my Eclipse editor:
Text Line 1
Text Line 2
I would like to concatenate the text into:
Text Line 1Text Line 2
My first idea was to search for carriage return character '\n' and replace it with '' to concatenate it.
I tried using the search function of Eclipse, but it does not recognize carriage return character.
Are there any other editor that can do this?
Eclipse does this if you:
turn on regular expression mode in search/replace
enter \R for the newline
Just use Edit -> Find/Replace, switch on the Regular Expressions checkbox, search for \n and replace it by space.
I tried it in Eclipse 3.4 and it worked well.
Short answer:
I decided to use \s++ as separator in multi-line search expressions (with regular expressions enabled) and \Qfoo\E to escape special characters if required.
Long answer:
As soru already answered, for any "Unicode linebreak sequence" a regular expression search with \R can be used.
A pure carriage return is represented by \r. Upper and lower case make a difference. \R represents any unicode linebreak sequence (for example \r\n).
I found this SO question because I wanted to search for a multi-line expression in Eclipse, including line breaks and tabs:
#Override
#Transient
In order to include the white spaces in my regular search expression I used (on Windows platform)
#Override\r\n\t*#Transient
Following expressions also work:
#Override\R\t*#Transient
#Override\s++#Transient
Please note that the second expression also matches #Override #Transient
without a line break, which is fine for me.
Following expressions did not! work for me:
#Override\r\t*#Transient
#Override\n\t*#Transient
Explanation of some regular expressions:
\R represents any unicode linebreak sequence (for example \r\n)
\s represents any white space
\t represents a tab
* matches zero or more occurrences
++ matches one ore more occurrences
\Q and \E escape wrapped content. Use them if your original multi line expression includes special regex characters, for example
\Q/**\E\s++\Q*\E
matches
/**
*
Also see:
Difference between \n and \r?
https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
Most find and replace tasks in editors (at least, TextPad) have the ability to replace via a regex. If you can find this option in eclipse, then just use that.
\r is the correct regular expression for carriage return. But Eclipse standard editor does not find it.
So use external editor, for example notepad++

Surround with quotation marks

How is it possible in Eclipse JDT to convert a multiline selection to String. Like the following
From:
xxxx
yyyy
zzz
To:
"xxxx " +
"yyyy " +
"zzz"
I tried the following template
"${line_selection}${cursor}"+
but that way I only get the whole block surrounded not each line separately. How can I achieve a multiline processing like commenting the selected block?
Maybe this is not what you mean but...
If I'm on a line in Eclipse and I enter double quotation marks, then inside that paste a multiline selection (like your xyz example) it will paste out like this:
"xxxx\n" +
"yyyy\n" +
"zzz"
Then you could just find/replace in a selection for "\n" to "", if you didn't intend the newlines.
I think the option to enable this is in Window/Preferences, under Java/Editor/Typing/, check the box next to "Escape text when pasting into a string literal". (Eclipse 3.4 Ganymede)
Find/Replace with the regex option turned on. Find:
^(.*)$
Replace with:
"$1" +
Well, the last line will have a surplus +, you have to delete it manually.
I would go with a Find/Replace eclipse in regexp mode:
Find:
^((?:\s(?)\S?)((?:\s(?![\r\n])))
Replace with
\1"\2"\3 +
Will preserve exactly whatever space or tabs you have before and after each string, and will surround them with the needed double-quotes. (last '+' needs to be removed)
This may not be exactly the answer you're looking for. You can easily achieve what you're asking by using the sed stream editor. This is available on all flavors of Unix, and also on Windows, by downloading a toolkit like cygwin. On the Unix shell command line run the command
sed 's/^/"/;s/$/"+/'
and paste the text you want to convert. On its output you'll obtain the converted text. The argument passed to sed says substitute (s) the beginning of a line (^) with a quote, and substitute (s) the end of each line ($) with a quote and a plus.
If the text you want to convert is large you may want to redirect sed's input and output through files. In such a case run something like
sed 's/^/"/;s/$/"+/' <inputfile >outputfile
On Windows you can also use the winclip command of the Outwit tool suite to directly change what's in the clipboard. Simply run
winclip -p | sed 's/^/"/;s/$/"+/' | winclip -c
The above command will paste the clipboard's contents into sed and the result back into the clipboard.
Finally, if you're often using this command, it makes sense placing it into a shell script file, so that you can easily run it. You can then even assign an Eclipse keyboard shortcut to it.