vs code can't replace using regex group references under PCRE2 mode - regex

I'm trying to replace my std stl usage to EASTL and since i have a lot of cpp/h files, i'm relying in 'Search in Files' option of vs-code, with the following pattern:
((?<=#include \<)([^\/(.h)]+?)(?=\>))
This matches completely fine in regexr.com, in both match and replace and in vs code as well but needs the option of PCRE2 engine being enabled due backreferences use.
Trying to reference the matching group #1 using $1 under Search sidebar view simply doesn't work, and just adds "$1".
But if i search & replace with the same input for each file manually, it works as intended.
Thanks.

EDIT: The bug which prevented replace from working properly with lookarounds has been fixed, see capture group in regex not working. It is working in the Insider's Build and will presumably be included in v1.39.
However, your regex:
((?<=#include \<)([^\/(.h)]+?)(?=\>)) should be changed to:
((?<=#include <)([^\/(.h)]+?)(?=>)) note the removal of escapes before < and > and then it works in the Insider's Build as of this date.
[And the PCRE2 mode has been deprecated since the original question. So you do not need that option anymore, PCRE2 will be used automatically if needed.]
There is a similar bug when using search/replace with newlines and the replace just literally inserts a $1 instead of the capture group's value. This bug has been fixed in the latest Insider's Build, see multiline replace issue and issue: newlines and replace with capture groups.
But I tried your regex in the Insider's Build and it has the same result as you had before - it inserts the literal $1 instead of its value. It appears to be a similar bug but due to the regex lookarounds.
So I tried a a simpler, but I think still correct, regex without the lookarounds:
^(#include\s+<)([^\.\/]+?)(>)
and replace with $1EASTL/$2.h$3 and it works as expected.
.

Related

vscode Find and replace across files, backreference is repeated the if more than one match is on the same line

I am searching across files in vscode using the following regex expression
(?<=[a-zA-Z])_([a-zA-Z]) with $1
to replace Some_Random_Text with SomeRandomText
but vs code repeats the backreference value if they are on the same line as shown below S is repeated:
That sure looks like a bug to me. It does not happen in the Find in the current file widget. You should file an issue on this - probably due to the Search across files handling the lookbehind (which should be no problem since it is fixed-width).
In the meantime, you can easily remove the lookbehind part and use:
find: ([a-zA-Z])_([a-zA-Z])
replace: $1$2
which works as it should.

Notepad++ Regex Find/Replace Using Look Behind not Working

I have the following CSS markup.
.previous-container{
float:left;
}
.primary-commands {
float:right;
}
Using the regex syntax search (?<=[\s,;])([a-zA-Z\-]+): it highlights the CSS property name as expected, however, upon clicking replace nothing is replaced. I have tried using group token syntax in replace line e.g. $[nth group] and any plain literal string replacement. No matter my attempts it will not replace the matched string with anything. I am using notepad++ version 6.7.5. Perhaps there is something obvious I am missing here?
Based on comments to the original question here are some work-arounds that solved my problem.
Option #1
Replace the lookbehind portion of the regex statement (?<=[\s,;]) with a simple non-lookbehind group matching statement such as ([\s,;]). This will continue to limit search results to strings beginning with the specified characters in the lookbehind. The only caveat is that in my replacement string e.g. $1 $2 I would need to leave out the undesired matched characters that should not be a part of the replacement string.
Option #2
Use the "Replace All" button. It will perform replacements correctly when using a positive lookbehind in your regex statement as opposed to using the "Replace" button for single replacement.
I went with Options #1 only because it achieves what I need while allowing me to still perform a single replacement at a time. With larger documents I don't want to use "Replace All" until I have thoroughly tested my regex expression.

Notepad 2 insert character after regular expression search

I am having an issue with trying to figure out how to insert some text after I perform a regex search. I know there is a replace function, but I am not looking for that option, just inserting. The text editor I am using is Notepad2, but I am willing to try this in other text editors.
Here is the example that I have.
TEST|Test2|Test3|Test4
This is what I am looking for
Test|Test2|PrefixTest3|Test4
Notice that I am trying to insert the the phrase "Prefix" after the 2nd pipe and leave everything else alone.
I can successfully query the result by using this regex:
^[^|]*\|[^|]*|
But then I do not know how I can retain everything prior and after the search point. Any ideas?
You could simply use \K inorder to discard the previously matched characters.
^[^|]*\|[^|]*\|\K
Then replace the match with the string prefix.
DEMO
You may easily do that in Notepad2 using the regex-based Replace feature.
Find:       ^\([^|]*|[^|]*|\)
Replace: \1Prefix
Details:
^ - start of a line (Notepad2 never overflows line boundaries!)
\([^|]*|[^|]*|\) - Capturing group 1 matching a sequence of:
[^|]* - zero or more chars other than |
| - a literal (yes, no escaping is necessary, both escaped and unescaped | match a literal |) pipe symbol
[^|]*| - see above, gets to the second |.
The replacement contains a \1 backreference that inserts what was captured with the capturing group 1.
NOTE that Notepad2 regex engine is very limited. Here is what the Notepad2 documentation says:
Notepad2 supports only a limited subset of regular expressions, as provided by built-in engine of the Scintilla source code editing component. The advantage is that it has a very small footprint. There's currently no plans to integrate a more advanced regular expressions engine, but this may be an option for future development.
Note: Regular expression search is limited to single lines, only.
Also, you may refer to the inline comments inside Scintilla RESearch.cxx file describing the supported syntax. Bear in mind that the regex type used in the Notepad2 S&R tool is that of POSIX and not all of the described Scintilla regex features will work in the tool.
Note that Notepad2 does not seem to support alternation and limiting quantifiers (similar to Lua patterns), but \w matches Unicode letters together with ASCII ones. Sadly, I could not make ? quantifier work.
^([^|]*\|[^|]*\|)
Try this.Replace by $1prefix.See demo.Just capture the first group and then use it for replace.The first group can be accessed by $1.
http://regex101.com/r/pQ9bV3/11

why with look behind regex, the replacement is not working well in Notepad++?

In Notepad++, for example if the search regex is
(?<latex>\$[^\$]*\$)(?=[\x{4e00}-\x{9fa5}])
and the replace is
~\g{latex}~
then the replacement is working properly.
But if the search regex contains look behind expression like
(?<=[\x{4e00}-\x{9fa5}])(?<latex>\$[^\$]*\$)(?=[\x{4e00}-\x{9fa5}])
then replace
~\g{latex}~
doesn't work in Notepad++, why???
Actually, I found out that your named backreference is the actual problem. As per the documentation, you need to use the syntax $+{name} for a named capture reference in the replace. So that one should work:
(?<=[\x{4e00}-\x{9fa5}])(?<latex>\$[^\$]*\$)(?=[\x{4e00}-\x{9fa5}])
And replace with:
~$+{latex}~
So the first regex you had should not be working properly, but replacing with literal ~g<latex>~. Even so, I can't really be sure here since I'm using an older version of N++ and the docs could be out of date.
Though I think that the simplest would be that you don't use the capture group. The below should work fine:
(?<=[\x{4e00}-\x{9fa5}])\$[^$]*\$(?=[\x{4e00}-\x{9fa5}])
And replace with ~$0~.

regexIssueTracker not working in CruiseControl.net

I am trying to get an issueUrlBuilder to work in my CruiseControl.NET config, and cannot figure out why they aren't working.
The first one I tried is this:
<cb:define name="issueTracker">
<issueUrlBuilder type="regexIssueTracker">
<find>^.*Issue (\d*).|\n*$</find>
<replace>https://issuetracker/ViewIssue.aspx?ID=$1</replace>
</issueUrlBuilder>
</cb:define>
Then, I reference it in the sourceControl block:
<sourcecontrol type="vaultplugin">
...
<issueTracker/>
</sourcecontrol>
My checkin comments look like this:
[Issue 1234] This is a test comment
I cannot find anywhere in the build reports/logs/etc. where that issue link is converted to a link. Is my regex wrong?
I've also tried the default issueUrlBuilder:
<cb:define name="issueTracker">
<issueUrlBuilder type="defaultIssueTracker">
<url>https://issuetracker/ViewIssue.aspx?ID={0}</url>
</issueUrlBuilder>
</cb:define>
Again, same comments and no links anywhere.
Anyone have any ideas.
It looks like you're trying to match a potentially multiline comment by using .|\n instead of just ., which doesn't match newlines by default. Your first problem is that | has the lowest associativity of all regex constructs, so it's dividing your whole regex into the alternatives ^.*Issue (\d*). or \n*$. You would need to enclose the alternation in a group: (?:.|\n)*.
Another potential problem is that the lines might be separated by \r\n (carriage-return plus linefeed) instead of just \n. If CCNET uses the .NET regex engine under the hood, that won't be a problem because the dot matches \r. But that's not true of all flavors, and anyway, there's always a better way to match anything including newlines than (?:.|\n)*. I suggest you try
<find>^.*Issue (\d*)(?s:.*)$</find>
or
<find>(?s)^.*Issue (\d*).*$</find>
(?s) and (?s:...) are inline modifiers which allow the dot to match line separator characters.
EDIT: It looks like this is a known bug in CCNET. If the inline modifier doesn't work, try replacing . with [\s\S], as you would in a JavaScript regex. Example:
<find>^.*Issue (\d*)[\s\S]*$</find>