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.
Related
I have several html files (source code) which contain lots of text and include the code of many reports files linked to.
I need to replace every space ( ) in the filenames by the undescore sign (_). This replace must not affect the rest of the text.
The links all follow the same folder structure, but the filenames are all very different except their extension (.pdf)
For example, I have:
Please find the presentations:
href="/Portals/12/Documents/GE-Project/Amalty-WS/Joanna_POT Pb paint health econ env_RUS.pdf">Presentation 1
I need:
Please find the presentations:
href="/Portals/12/Documents/GE-Project/Amalty-WS/Joanna_POT_Pb_paint_health_econ_env_RUS.pdf">Presentation 1
In Windows, using TEXTPAD, I have tried find/replace using regex (/Amalty-WS/[a-z,A-Z,0-9,_, ,]*\.pdf) but can't figure out how to replace only the spaces.
You can use
(?:\G(?!^)|/Amalty-WS/)[^"\s]*\K\s(?=[^"]*")
Replace with _.
See the regex demo.
Details:
(?:\G(?!^)|/Amalty-WS/) - either the end of the previous successful match (\G(?!^)) or /Amalty-WS/ string
[^"\s]* - zero or more chars other than " and whitespace
\K - match reset operator that discards text matched so far
\s - a whitespace
(?=[^"]*") - followed with zero or more chars other than " and then a ".
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.
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.
I am trying to match lines in a file that contain only a single / so my thought is i can search for a string of any length that doesn't contain a / and then match exactly one / and then match another string of any length not containing a / and finally ending with a line break.
My attempt at this was [^/]*/[^/]*$. however this doesn't seem to work.
I went ahead and tried matching just parts of this pattern and started by just trying to match strings of any length not containing a / which I would think should be just [^/]* but this isn't working.
I am pretty familiar with regex but not as familiar with using it in vim so firstly, am I putting in my regex wrong for using vim? and secondly, if my input for vim is correct, then what is wrong with my regex?
You may search for all the lines matching your pattern using
:g/^[^\/]*\/[^\/]*$
Note that g will match all occurrences, backslashes need escaping here, and the pattern matches
^ - start of a line
[^\/]* - 0+ chars other than /
\/ - a /
[^\/]* - 0+ chars other than /
$ - end of a line.
Note that [^\/]* (negated bracket expression) won't match a line break sequence in Vim, unlike in text editors like Sublime Text 3 or Notepad++, thus, it will match exactly what you need.
Note that you may avoid escaping backslashes if you select another delimiter. See the Vim regex reference:
Frequently you need to do S&R in a text which contains UNIX file paths - text strings with slashes ("/") inside. Because S&R command uses slashes for pattern/replacement separation you have to escape every slash in your pattern, i.e. use "\/" for every "/" in your pattern... To avoid this so-called "backslashitis" you can use different separators in S&R.
So, you may also use :g~^[^/]*/[^/]*$~, or :g#^[^/]*/[^/]*$# as Amadan suggests.
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.