Remove any blank or whitespaced lines using regex - regex

I am trying to find and remove any blank or whitespaced lines in OpenOffice document using regex.
Currently I can do it in two steps:
Search for ^$ and replace with nothing.
This will remove any empty lines.
Search for ^\s*$ and replace with nothing.
This will remove any lines which contains only spaces or tabs.
Important note: From my point of view, this 2nd version should also remove any empty lines (as 1st version), but actually it doesn't.
So, there are actually two questions.
For what reason second regex matches only lines with spaces and tabs, but don't match empty lines?
Is there way to combine first and second version to achieve desired result in one step? Here what I tried: ^$|^\s*$ and (^|^\s*)$. But it doesn't work. It matches only whitespaced lines, but not empty ones.
Text for test:
Just for example, I changed spaces to dots
and tabs to dashes.
aa
..........................
-------------------
aaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaa
Desired result:
aa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaa

AltSearch can do this as a single step using a Batch script. In the AltSearch dialog, click on Batch >>. Then Edit the file and paste the following code at the end.
[Name] Remove any blank or whitespaced lines
; Remove any lines which contains only spaces or tabs.
[Find]^\s*$
[Replace]
[Parameters] MsgOff Regular
[Command] ReplaceAll
; Remove any empty lines.
[Find]^$
[Replace]
[Parameters] MsgOff Regular
[Command] ReplaceAll
[End]
Now, save the text file and click Refresh. Finally, click on Remove any blank or whitespaced lines and press Execute.
This produces the desired result and shows a single dialog:
Batch 'Remove any blank or whitespaced lines' is ended.
10 replacements have been done.

Since the title of your question reads like asking for a pure regex (which is why I found it):
\s(?=\s)
Simply replace its matches with nothing - see Regex101 and Regexr.

Related

Regular expression to replace two blank lines with one blank line

I'd like to use Xcode's find and replace by regex to remove unnecessary blank lines in my code. In other words I want to replace this:
NSString *helloString = #"Hello";
helloString = [helloString stringByAppendingString:#" World"];
With this:
NSString *helloString = #"Hello";
helloString = [helloString stringByAppendingString:#" World"];
I tried typing control+q return control+q return in the search box (inspired by this answer but it didn't match anything.
What should the regex be?
Copy 2 of the empty lines from your source, press APPLE-OPTION-F to display the searchbar and paste the two lines into the first field.
Then go the second field and enter OPTION-RETURN
Then hit the replace button on the right
Be aware that this matches only lines with the same indendation/spaces/tabs as your copied line. If you need something more flexible better use a texteditor supporting regexen.
As possible regex could be something like "\n.*\n"
I don't know regex very well. I'd just use a simple find and replace of literal strings. Select a blank line by putting your cursor on the last character of a line and pressing shift right-arrow to select the line break. Then bring up a replace dialog and paste 2 blank lines into the find field, and 1 blank line into the replace field.
Xcode doesn't support regex matches across multiple lines, per this mailing-list posting from Apple employee #8 Chris Espinoza.
You could do a two-step process:
Do a regex search like this, to remove whitespace characters from otherwise empty lines:
Find: ^\s+$
Replace: leave blank.
Do a textual (non-regex) search like this, to replace two empty lines with one:
Select two blank lines and go to Edit > Find > Use Selection For Find (Command-E)
Select a single blank line and go to Edit > Find > Use Selection For Replace (Command-Shift-E)
For such tasks and other formatting perhaps you'd like to check uncrustifyX, it's quite easy to use and extensive.

Unwrap text in Sublime Text 2

I'd like to unwrap lines so that I can turn them from lines with hard line-breaks to no line breaks.
Specifically, this means that contiguous runs of lines with non-whitespace should be joined together Essentially, any \n with no whitespace on either side should be replaced with a single space. Other linebreaks shouldn't get touched.
I feel like it ought to be a search-and-replace with a search string something like (?!\n)\n(?!\n) -> , but that doesn't work, as it doesn't match anything.
Is there an ST2 built-in command for this?
any \n with no whitespace on either side
(?<!\s)\n(?!\s)
other linebreaks shouldn't get touched.
(?<!(?:\s|\n))\n(?!\s)
Replace with ''
As #flow mentioned, there are built-ins for that task. Just select the lines you want to join and press Ctrl + J.
And your way should works too. Only you missed a bit. It should be (?<!\n)\n(?!\n)
The following solution works best for text copied from a console log with 80 columns. It only removes \n if the line touches the last column.
Find:
(.{80})\n
Replace:
$1

Style sheet issuewith blank lines

When i opened my style sheet using FTP, it seems lots of blank lines and white spaces.
how do i remove that, please help ..
an example line is here
uploaded css :
h1{
color:#01486d;
font-size:42px;
} /* no blank lines */
when i opened the same style sheet via FTP it is like
h1 {
color:#01486d;
font-size:42px;
}/* lots of white spaces and blank lines */
some one suggest regular expression but i dont know how to use regular expression with dream viewer.
please help
thanks
If you open up Vi/Vim, you can delete blank lines with the following code:
:g/^$/d
and tabs can be deleted with this bit of code:
:g/\t/d
Breakdown:
Hitting escape and then : will get you to the command prompt.
The actual command is g/\t/d
The g searches the entire document
Everything in between the / / is the regex pattern to look for, in this case lines lines that begin (^) with the end of line marker ($) and tabs (\t). The d represents the action to take. In this case, delete the line.
Commands > Apply Source Formatting.
Regular expression to find blank lines:
^$
Regular expression to find multiple blank lines in a row:
[\n]{2,}
You can just delete the first, or replace the second with \n.

Remove double spacing

Sometimes, copy pasting code from my email makes everything have an extra blank line.
For example
1: hi
2:
3: hello
4:
Is there a way to target these empty lines with regex and delete them?
I'm using notepad++ with the search(with regex) and replace capability.
Because Notepad++ regex operates only line by line, without a multi-line mode, you can't remove entire lines with regex alone. This is no longer true as of Notepad++ 6.0, which now uses PCRE as its regex engine and allows for multi-line replacements. See this answer for more info.
The TextFX plugin that Notepad++ ships with allows you to remove blank lines without using regex. Just highlight your entire document (Ctrl+A) and do TextFX > TextFX Edit > Delete Blank Lines. If your selection or document begins and/or ends with a blank line though, those lines won't be removed automatically — but removing those is just a matter of:
Ctrl+Home
Del
Ctrl+End
Backspace
To remove double spacing in Notepad++ (I'm using v7.8.4) go to: Edit, Line Operations, Remove Empty Lines.
I don't have notepad++, but the regular expression "^$" (without the quotes) matches only blank lines. Perhaps notepad++ will allow you to replace matches of that regular expression with the empty string, thus removing the blank lines.
Search > Replace...
Search Mode = Extended
Find what : \r\n\r\n
Replace with : \r\n

Removing empty lines in Notepad++

How can I replace empty lines in Notepad++? I tried a find and replace with the empty lines in the find, and nothing in the replace, but it did not work; it probably needs regex.
There is now a built-in way to do this as of version 6.5.2
Edit -> Line Operations -> Remove Empty Lines or Remove Empty Lines (Containing Blank characters)
You need something like a regular expression.
You have to be in Extended mode
If you want all the lines to end up on a single line use \r\n. If you want to simply remove empty lines, use \n\r as #Link originally suggested.
Replace either expression with nothing.
There is a plugin that adds a menu entitled TextFX. This menu, which houses a dizzying array of quick text editing options, gives a person the ability to make quick coding changes. In this menu, you can find selections such as Drop Quotes, Delete Blank Lines as well as Unwrap and Rewrap Text
Do the following:
TextFX > TextFX Edit > Delete Blank Lines
TextFX > TextFX Edit > Delete Surplus Blank Lines
notepad++
Ctrl-H
Select Regular Expression
Enter ^[ \t]*$\r?\n into find what, leave replace empty. This will match all lines starting with white space and ending with carriage return (in this case a windows crlf)
Click the Find Next button to see for yourself how it matches only empty lines.
Press ctrl + h (Shortcut for replace).
In the Find what zone, type ^\R ( for exact empty lines) or ^\h*\R ( for empty lines with blanks, only).
Leave the Replace with zone empty.
Check the Wrap around option.
Select the Regular expression search mode.
Click on the Replace All button.
You can follow the technique as shown in the following screenshot:
Find what: ^\r\n
Replace with: keep this empty
Search Mode: Regular expression
Wrap around: selected
NOTE: for *nix files just find by \n
This worked for me:
Press ctrl + h (Shortcut for replace)
Write one of the following regex in find what box.
[\n\r]+$ or ^[\n\r]+
Leave Replace with box blank
In Search Mode, select Regex
Click on Replace All
Done!
In notepad++ press CTRL+H , in search mode click on the "Extended (\n, \r, \t ...)" radio button then type in the "Find what" box: \r\n (short for CR LF) and leave the "Replace with" box empty..
Finally hit replace all
Well I'm not sure about the regex or your situation..
How about CTRL+A, Select the TextFX menu -> TextFX Edit -> Delete Blank Lines and viola all blank line gone.
A side note - if the line is blank i.e. does not contain spaces, this will work
1) Ctrl + H ( Or Search 🠆 Replace..) to open Replace window.
2) Select 'Search Mode' 'Regular expression'
3) In 'Find What' type ^(\s*)(.*)(\s*)$ & in 'Replace With' type \2
^ - Matches start of line character
(\s*) - Matches empty space characters
(.*) - Matches any characters
(\s*) - Matches empty spaces characters
$ - Matches end of line character
\2 - Denotes the matching contend of the 2nd bracket
Refer https://www.rexegg.com/regex-quickstart.html for more on regex.
You can search for the following regex: ^(?:[\t ]*(?:\r?\n|\r))+ and replace it with empty field
Ctrl+H.
find - \r\r
replace with - \r.
This obviously does not work if the blank lines contain tabs or blanks. Many web pages (e.g. http://www.guardian.co.uk/) contain these white lines, as a result of a faulty HTML editor.
Remove white space using regular expression as follows:
change pattern: [\t ]+$
into nothing.
where [\t ] matches either tab or space. '+' matches one or more occurrences, and '$' marks the end of line.
Then use notepad++/textFX to remove single or extra empty lines.
Be sure that these blank lines are not significant in the given context.
Edit >> Blank Operations >> Trim Leading and Trailing Spaces (to remove black tabs and spaces in empty lines)
Ctrl + H to get replace window and replace pattern: ^\r\n with nothing (select regular expression)
Note: step 1 will remove your code intendation done via tabs and blank spaces
Sometimes \n\r etc not work, here to figure it out, what your actually regular expression should be.
Advantage of this trick: If you want to replace in multiple file at once, you must need this method. Above will not work...
CTRL+A, Select the TextFX menu -> TextFX Edit -> Delete Blank Lines as suggested above works.
But if lines contains some space, then move the cursor to that line and do a CTRL + H. The "Find what:" sec will show the blank space and in the "Replace with" section, leave it blank.
Now all the spaces are removed and now try CTRL+A, Select the TextFX menu -> TextFX Edit -> Delete Blank Lines
/n/r assumes a specific type of line break. To target any blank line you could also use:
^$
This says - any line that begins and then ends with nothing between. This is more of a catch-all. Replace with the same empty string.
I did not see the combined one as answer, so search for ^\s+$ and replace by {nothing}
^\s+$ means
^ start of line
\s+ Matches minimum one whitespace character (spaces, tabs, line breaks)
$ until end of line
This pattern is tested in Notepad++ v8.1.1
It replaces all spaces/tabs/blank lines before and after each row of text.
It shouldn't mess with anything in the middle of the text.
Find: ^(\s|\t)+|(\s|\t)+$
Replace: leave this blank
Before:
_____________________________________
\tWORD\r\n
\r\n
\tWORD\s\tWORD\s\t\r\n
\r\n
\r\n
WORD\s\s\tWORD\t\sWORD\s\r\n
\t\r\n
\s\s\s\r\n
WORD\s\sWORD\s\s\t\r\n
____________________________________
After:
_____________________________________
WORD\r\n
WORD\s\tWORD\r\n
WORD\s\s\tWORD\t\sWORD\r\n
WORD\s\sWORD
_____________________________________
A few of the above expressions and extended expressions did not work for me, but the regular expression "$\n$" did.
An easy alternative for removing white space from empty lines:
TextFX>TextFX Edit> Trim Trailing Spaces
This will remove all trailing spaces, including trailing spaces in blank lines.
Make sure, no trailing spaces are significant.
this work for me:
SEARCH:^\r
REPLACE: (empty)