visual studio 2013 - how to match specific line with regex? - regex

I want to match a specific line with regex in Visual Studio 2013 and delete the whole line, the first three characters in this kind of line are #, whitespace and a number.
so, how to write the regex to match these lines? thanks!

To match a # followed by whitespace and then number, use:
/^\#[\ \t]+\d+.*$/gm
You might need global and multi-line modifier.
Using \s should be avoided as it matches \n and \r too.
This will match all those lines that start with # followed by whitespace and number.

Related

regex difference between vscode and visual studio

regex difference between vscode and visual studio
starting with
line1
line2
find: ^(.+)$
replace: "$1",
In vscode it works as expected, resulting in
"line1",
"line2",
In studio, doesn't seem to work, resulting in
"line1
",
"line2
",
Which one is correct? I assume vscode.
TL;DR: Use ^(.*[^\r\n]) to match a whole line without EOL characters.
According to the Docs:
Purpose
Expression
Example
Match any single character (except a line break)
.
a.o matches "aro" in "around" and "abo" in "about" but not "acro" in "across"
Anchor the match string to the end of a line
\r?$
car\r?$ matches "car" only when it appears at the end of a line
Anchor the match string to the end of the file
$
car$ matches "car" only when it appears at the end of the file
However, some of that doesn't seem to hold true for some reason (i.e., . does match a line break and .$ does match the end of any line). All of the following patterns will match from the beginning to the end of the line including EOL characters: ^.+, ^.+$, ^.+\r?$.
I have noticed this behavior in VS2017 before and I'm not sure why it happens but I was able to get around it using something like the following:
^(.*[^\r\n])
Note: You can also get rid of the capturing group and replace with "$0",.
In VSCode regex patterns, a dot . matches any char but any line break chars.
In .NET regex used in Visual Studio, a dot matches any char but a newline, LF, char.
This difference explains the results you get and you can't call them right or wrong, these are just regex engine differences.
Note you would not have noticed any difference between the two engines if you had used LF-only line endings, but Visual Studio in Windows uses CRLF endings by default.
In order to wrap a whole line with double quotes using .NET regex, just exclude both LF and CR (carriage return) symbols from matching by replacing the dot with a [^\r\n] negated character class:
^[^\r\n]+
And replace with "$&", pattern where $& refers to the whole match.
You may get rid of the capturing group in the VSCode regex and use the same replacement pattern as in .NET, too.

Multi-line regular expressions in Visual Studio Code

I cannot figure a way to make regular expression match stop not on end of line, but on end of file in VS Code? Is it a tool limitation or there is some kind of pattern that I am not aware of?
It seems the CR is not matched with [\s\S]. Add \r to this character class:
[\s\S\r]+
will match any 1+ chars.
Other alternatives that proved working are [^\r]+ and [\w\W]+.
If you want to make any character class match line breaks, be it a positive or negative character class, you need to add \r in it.
Examples:
Any text between the two closest a and b chars: a[^ab\r]*b
Any text between START and the closest STOP words:
START[\s\S\r]*?STOP
START[^\r]*?STOP
START[\w\W]*?STOP
Any text between the closest START and STOP words:
START(?:(?!START)[\s\S\r])*?STOP
See a demo screenshot below:
To matcha multi-line text block starting from aaa and ending with the first bbb (lazy qualifier)
aaa(.|\n)+?bbb
To find a multi-line text block starting from aaa and ending with the last bbb. (greedy qualifier)
aaa(.|\n)+bbb
If you want to exclude certain characters from the "in between" text, you can do that too. This only finds blocks where the character "c" doesn't occur between "aaa" and "bbb":
aaa([^c]|\n)+?bbb

Eclipse Add text to first line of all files

I need to add text to first line of all my JSP's in eclipse, this is the regex I a using \A.* but some how it selects the first line, I just want to prepend text to the start of the file. any help will be very much appreciated.
The .* pattern matches any 0+ chars other than line break characters, so it matches the first line.
It seems that Eclipse Find/Replace regex feature does not match entirely zero-width patterns (e.g. (?=,) will not find and insert a text before commas).
A workaround is to match and capture some text with (...) (where ... stand for a consuming pattern) capturing group and use $1 in the replacement pattern to reinsert the matched text.
Use
\A(.*)
Replace with MY_NEW_TEXT_HERE_AT_THE_START_OF_FILE$1.

Delphi regular expression, ignoring LineFeed in matched text

I want to search for the following regular expression
^[ ]*,$
in the following text :
,[LF]
,[LF]
My problem is that Delphi finds the expression, but the matched text doesn't include the LF.
Effectively I want to removes the lines from my source code.
I'am using TPerlRegEx with delphiXe8
In the example [LF] is the linefeed ($0D $0A)
I Tested several flags combinaisons in TPerlRegExOptions
This works perfectly in SublimeText 3
What am I missing ?
If you are using a PCRE regex, you can match zero or more spaces at the stsart of a line followed with a comma followed with a newline sequence with
(?m)^[ ]*,\R
See the regex demo. Note that (?m) is a multiline modifier making ^ match a location at the beginning of a line (after \n). \R matches any newline sequence.
Add a ? after \R to also match the last line in the text that has no newline sequence at the end.

How do I remove trailing whitespace using a regular expression?

I want to remove trailing white spaces and tabs from my code without
removing empty lines.
I tried:
\s+$
and:
([^\n]*)\s+\r\n
But they all removed empty lines too. I guess \s matches end-of-line characters too.
UPDATE (2016):
Nowadays I automate such code cleaning by using Sublime's TrailingSpaces package, with custom/user setting:
"trailing_spaces_trim_on_save": true
It highlights trailing white spaces and automatically trims them on save.
Try just removing trailing spaces and tabs:
[ \t]+$
To remove trailing whitespace while also preserving whitespace-only lines, you want the regex to only remove trailing whitespace after non-whitespace characters. So you need to first check for a non-whitespace character. This means that the non-whitespace character will be included in the match, so you need to include it in the replacement.
Regex: ([^ \t\r\n])[ \t]+$
Replacement: \1 or $1, depending on the IDE
The platform is not specified, but in C# (.NET) it would be:
Regular expression (presumes the multiline option - the example below uses it):
[ \t]+(\r?$)
Replacement:
$1
For an explanation of "\r?$", see Regular Expression Options, Multiline Mode (MSDN).
Code example
This will remove all trailing spaces and all trailing TABs in all lines:
string inputText = " Hello, World! \r\n" +
" Some other line\r\n" +
" The last line ";
string cleanedUpText = Regex.Replace(inputText,
#"[ \t]+(\r?$)", #"$1",
RegexOptions.Multiline);
Regex to find trailing and leading whitespaces:
^[ \t]+|[ \t]+$
If using Visual Studio 2012 and later (which uses .NET regular expressions), you can remove trailing whitespace without removing blank lines by using the following regex
Replace (?([^\r\n])\s)+(\r?\n)
With $1
Some explanation
The reason you need the rather complicated expression is that the character class \s matches spaces, tabs and newline characters, so \s+ will match a group of lines containing only whitespace. It doesn't help adding a $ termination to this regex, because this will still match a group of lines containing only whitespace and newline characters.
You may also want to know (as I did) exactly what the (?([^\r\n])\s) expression means. This is an Alternation Construct, which effectively means match to the whitespace character class if it is not a carriage return or linefeed.
Alternation constructs normally have a true and false part,
(?( expression ) yes | no )
but in this case the false part is not specified.
[ |\t]+$ with an empty replace works.
\s+($) with a $1 replace also works, at least in Visual Studio Code...
To remove trailing white space while ignoring empty lines I use positive look-behind:
(?<=\S)\s+$
The look-behind is the way go to exclude the non-whitespace (\S) from the match.
To remove any blank trailing spaces use this:
\n|^\s+\n
I tested in the Atom and Xcode editors.
In Java:
String str = " hello world ";
// prints "hello world"
System.out.println(str.replaceAll("^(\\s+)|(\\s+)$", ""));
You can simply use it like this:
var regex = /( )/g;
Sample: click here