regex difference between vscode and visual studio - regex

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.

Related

vs-code: Append a text to multiple opened files using regex?

I'm trying to append some text to multiple files. Is this possible in VS-Code with
Edit->"Replace in Files" using regex?
\z (only the end of text) is not working here - invalid regular expression.
You may match the end of line that has no character immediately to the right:
$(?![\w\W])
Here, $ matches an end of line position and (?![\w\W]) is a negative lookahead that fails the match if any char appears immediately to the right of that location.
See the regex demo where m flag is enabled and makes $ match end of line positions, as in Visual Studio Code, and due to (?![\w\W]) it only matches at the very end of the text.

Match the beginning of a file in a VSCode regex search

I'm trying to match the beginning of a file in a VSCode regex search to find and remove the following pattern:
//
Anything else to leave in place
etc.
I'd like to remove the first line in all files that contain //\n as the first characters. However when I perform a regex search for //\n it matches all occurences in all files obviously, regardless of their position in the file.
The regex language mentions the existence of \A to match the beginning of a line, or file in some editors, but VSCode rejects this regex as invalid: \A//\n.
So my question is, how can I match the beginning of a file in VSCode to achieve what I need?
The beginning of a file in Visual Studio Code regex can be matched with
^(?<!\n)
^(?<![\w\W])
^(?<![\s\S\r])
You may use
Find What: ^//\n([\s\S\r]*)
Replace With: $1
Or, since nowadays VSCode supports lookbehinds as modern JS ECMAScript 2018+ compatible environments, you may also use
Find What: ^(?<![\s\S\r])//\n
Replace With: empty
If you wonder why [\s\S\r] is used and not [\s\S], please refer to Multi-line regular expressions in Visual Studio Code.
Details
^ - start of a line
// - a // substring
\n - a line break
([\s\S\r]*) - Group 1 ($1): any 0 or more chars as many as possible up to the file end.
The ^(?<![\s\S\r])//\n regex means:
^(?<![\s\S\r]) - match the start of the first line only as ^ matches start of a line and (?<![\s\S\r]) negative lookbehind fails the match if there is any 1 char immediately to the left of the current location
//\n - // and a line break.

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.

visual studio 2013 - how to match specific line with 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.

Regular expression to match last line break in file

In my quest to learn flex I'm having a scanner echo input adding line numbers.
After every line I display a counter and increment it.
Trouble is there is always a lone line number at the end of the display.
I need a regex that will ignore all line breaks except for the last one.
I tried [\n/<<EOF>>] to no avail.
Any thoughts?
I don't know what regex engine uses Flex but you can use this regex:
\z
Working demo
\z assert position at the very end of the string.
Matches the end of a string only. Unlike $, this is not affected by
multiline mode, and, in contrast to \Z, will not match before a
trailing newline at the end of a string.
If above regex doesn't work then you can use this one:
(?<=[\S\s])$
Working demo
Edit: since flex seems to work slightly different than other regex engines you could use this regex:
[\s\S]$
To get the latest character of each line. Then you can iterated over all lines until get the last one. Here you have an online flex regex engine tool:
http://ryanswanson.com/regexp/#start
Try below regex, It will search for a new line character at the end of the line.
\n$
Have you tried simply doing:
\n$
Debuggex Demo
The \n matches the newline, the $ matches end of string.