Regex with varying whitespaces - regex

I'm trying to write Regex to find lines in a files where commands are not using absolute paths. Unfortunately, there are spacing issues with these lines.
// PATHS
pathmunge /absolute/path
pathmunge ~not/absolute/path
pathmunge /absolute/path
// matches
pathmunge ~not/absolute/path
My expression matches the lines where there are spaces at the beginning and before pathmunge string, but it doesn't find the lines with variable-length whitespace but without a "/" as the next non-whitespace character.
So far, I have:
^(?=\s+pathmunge)\s+(?!\/).*$
Any help is appreciated.

For a regex, I would do:
^(?:[[:blank:]]*pathmunge[[:blank:]]+([^\s\/].*$))
Demo
The key element is the [^\s\/] which matches a single character other than a horizontal space or a \n or / start of any absolute path.

Remove the lookahead and make the space possessive:
^\s*pathmunge\s++(?!\/).*$
Demo & explanation

Related

Remove trailing whitespace at the end of aspx file

I am trying to remove trailing whitespace including \r and \n at the end of aspx files by using Find and Replace using the pattern
\s+(?!.)
trying to replace whitespace followed by nothing with nothing.
The result is that everything will come on the same line.
Why?
I also tried \s+$ with the same result.
You may add a negative lookahead to the end of your current pattern:
(\s+\r?\n)+$(?!.)
This will ensure that only final lines with whitespace only are matched. See the demo here.

Find lines without specified string and remove empty lines too

So, I know from this question how to find all the lines that don't contain a specific string. But it leaves a lot of empty newlines when I use it, for example, in a text editor substitution (Notepad++, Sublime, etc).
Is there a way to also remove the empty lines left behind by the substitution in the same regex or, as it's mentioned on the accepted answer, "this is not something regex ... should do"?
Example, based on the example from that question:
Input:
aahoho
bbhihi
cchaha
sshede
ddhudu
wwhada
hede
eehidi
Desired output:
sshede
hede
[edit-1]
Let's try this again: what I want is a way to use regex replace to remove everything that does not contain hede on the text editor. If I try .*hede.* it will find all hede:
But it will not remove. On a short file, this is easy to do manually, but the idea here is to replace on a larger file, with over 1000+ lines, but that would contain anywhere between 20-50 lines with the desired string.
If I use ^((?!hede).)*$ and replace it with nothing, I end up with empty lines:
I thought it was a simple question, for people with a better understanding of regex than me: can a single regex replace also remove those empty lines left behind?
An alternative try
Find what: ^(?!.*hede).*\s?
Replace with: nothing
Explanation:
^ # start of a line
(?!) # a Negative Lookahead
. # matches any character (except for line terminators)
* # matches the previous token between zero and unlimited times,
hede # matches the characters hede literally
\s # matches any whitespace character (equivalent to [\r\n\t\f\v ])
? # matches the previous token between zero and one times,
Using Notepad++.
Ctrl+H
Find what: ^((?!hede).)*(?:\R|\z)
Replace with: LEAVE EMPTY
CHECK Match case
CHECK Wrap around
CHECK Regular expression
UNCHECK . matches newline
Replace all
Explanation:
^ # beginning of line
((?!hede).)* # tempered greedy token, make sure we haven't hede in the line
(?:\R|\z) # non capture group, any kind of line break OR end of file
Screenshot (before):
Screenshot (after):
Have you tried:
.*hede.*
I don't know why you are doing an inverse search for this.
You can use sed like:
sed -e '/.*hede.*/!d' input.txt

Notepad++ N text lines separated by blank lines?

I searched a bit, but didn't find a solution for this specific situation. I need to combine groups of non-blank lines into single lines, while preserving the blank lines. For example, the input:
Hi, My name is
Max
What are you
doing
Right now?
Hi
Hello
World
should be output as:
Hi, My name is Max
What are you doing Right now?
Hi
Hello World
Thanks in advance to all who respond.
You could try replacing
(?<![\n\r])[\n\r](?![\n\r])
With a space, as demonstrated here
Explanation -
(?<![\n\r]) is a negative look-behind which tells the regex that anything to be matched must not be preceded by a newline or by a carriage return (just take it as a newline)
[\n\r] is the newline or carriage return which is matched (and later replaced with a space)
(?![\n\r]) is a negative look-ahead that tells the regex that any newline to be matched should not be followed by another newline or carriage return.
In essence, this replaces the blank, new lines which are not followed by another newline - with a space.
You can try this too,
(?m)(?!^\s*$)(^[^\n]*)\n(?!^\s*$)
Demo,,, in which matches all lines which are not empty and not followed by empty line and remove all matched newline character (\n).
But, in notepad++, you must consider carrige return(\r) with newline(\n). Thus,
(?m)(?!^\s*$)(^[^\n]*)\r\n(?!^\s*$)

Regex find STRING, but ignore lines, that have //.* before STRING [duplicate]

I try to verify a CSV file where we had problems with line breaks.
I want to find all lines not starting with a ".
I am trying with /!^"/gim but the ! negation is not working.
How can I negate /^"/gim properly?
In regex, the ! does not mean negation; instead, you want to negate a character set with [^"]. The brackets, [], denote a character set and if it starts with a ^ that means "not this character set".
So, if you wanted to match things that are not double-quotes, you would use [^"]; if you don't want to match any quotes, you could use [^"'], etc.
With Notepad++, you should be able to search with the following to find lines that don't start with the " character:
^[^"]
If you want to highlight the full line, use:
^[^"].*
In Notepad++ you can use the very usefull negative lookahead
In your case you can try the following:
^(?!")
If you want to match wholes lines add .+ or .{1,7} or anything e.g.:
^(?!").*
will also match empty lines.
Explanation part
^ line start
(?!regexp) negative lookahead part: this means that if the regexp match, the result will not be shown
Step 1 - Match lines. Find dialog > Mark tab, you can bookmark lines that match.
Step 2 - Remove lines bookmarked OR Remove lines not bookmarked. Search > Bookmark > Remove Unmarked Lines or Remove Bookmarked lines

Regular expression matching space but at the end of line

I'm trying to replace multiple spaces with a single one, but at the start of the line.
Example:
___abc___def__
___ghi___jkl__
should turn to
___abc_def__
___ghi_jkl__
Note that I've replaced space with underscore
A simple search using the following pattern:
([^\s])\s+
matches the space at the end of the first line up to the space at the beginning of the next one.
So, if I replace with \1_, I get the following:
___abc_def_ghi_jkl
And that is absolutely not what I expect and regex engines, e.g., PowerGREP or the one in Visual Studio, don't behave that way.
If you want to match only horizontal spaces, use \h:
Find what: (?<=\S)\h+(?=\S)
Replace with: (a space)
There are several possible interpretations of the question. For each of them the replacement will be a single space character.
If spaces is plural and means space characters but not tabs then use
a find string of (^ {2,})|( {2,}$).
If spaces is plural and should includes tabs then use a find string
of (^[ \t]{2,})|([ \t]{2,}$).
If any leading or trailing spaces and tabs (one or more) is to be
replaced with a space then use a find string of (^[ \t]+)|([ \t]+$).
The general form of each of these is (^...)|(...$). The | means an alternation so either the preceding or the following bracketed expression can match. Hence the find what text can match either at the beginning or the end of a line. The ... varies depending on exactly what needs to be matched. Specifying [ \t] means only the two characters space and tab, whereas \s includes the line-end characters.
Ok, so the intention was to replace this:
Hey diddle diddle, \n<br/>
The Cat and the fiddle,\n
with this:
Hey diddle diddle,\n<br/>
The Cat and the fiddle,\n
A slightly modified version of Toto's answer did the trick:
(?<=\S)\h+(?=\S)|\s+$
finding any space(s) between word-characters and trailing space at the end of the line.