Are named capture groups supported? If so, how to engage? - regex

I understand that VSCode uses the JavaScript regex engine for its functionality.
The latest JavaScript specification allows for named capture groups to be used.
However, I am at a loss in understanding whether this is enabled in VSCode v1.43?
I am using the following notations in the general find command:
(?<name-of-capture>pattern to find)( other stuff )(\k<name-of-capture>)
(?<name-of-capture>pattern to find)( other stuff )(\g<name-of-capture>)
I have also used the combinations of \k'name' and \g'name' and these have no effect.
If anyone has insights into this I would appreciate to hear.

If you want to use an inline backreference, they work in VSCode.
(?<group>[a-z]+) \d+ \k<group>
matches abc 1 abc.
However, new JavaScript-like $<group> replacement does not work, .NET-style replacement backreference, ${group}, does not work either, probably, due to the issue referred to by #JW.
NOTE: They say they need 20 votes on the issue and there are 3 days to go before they close the issue and turn down the suggestion to introduce backreferences in replacement. If you want this feature to be implemented, please consider voting for that issue.

Related

How can I specify the regular expression dialect in IntelliJ IDEA?

I have a file which is in Java's regular expression dialect:
# Prevents matching at the second half of a version number and things like
# 1.16.2 splitting into 1.1 and 6.2
(?<![._\-\d])
(?<sign>-)?
(?<integerPart>\d+(?:,\d+)*)
(
(?<fractionalPart>\.\d+)?
(?<suffix>[kKMG%])?
# Prevents matching at the first half of a version number
(?![._\-\d])
|
# Note how this one does _not_ include '.' because we wanted to deal with
# integers with a period after them. This may change?
(?![_\-\d])
)
IDEA gives me errors on all the groups, saying: "This named group syntax is not supported in this regex dialect".
But when I edit settings for this inspection there is just one checkbox.
Questions:
What dialect is the default anyway? I'm mildly surprised that it isn't the Java Pattern one
How do I configure this to use Java one? Is there a magic comment I can put in the file to hint at the format which IDEA and maybe even other text editors would recognise?
It looks like a known bug in IntelliJ IDEA. There is no way to change the dialect at the moment.

End RegEx after certain digit

I am working on a filter based upon browser version and am having a little trouble. It has to be in RegEx which loves to encompass everything possible.
I want to select:
12.0
8.0
18.0.1025.168
The problem I am having (looking at the 12.0 specifically however it is a problem for all 3) is that it is selecting things other than 12.0 as well. I have been trying to use negated sets and non-capturing groups however it just isn't quite working.
Currently I have: ((?:18.0.1025.168[^.]|(12.0)[^.]|(?:8.0)[^.]))
I have used \d in the negated sets however it seems as though I have to choose \d or . because it does not allow for special characters within the set.
Things that I need to make sure are not selected include any variation of the following, (the 9's could be any number)
9.12.0
912.0
92.09
12.0.9
Any input of what I should look into or another symbol I could use would be greatly appreciated. Also, if needed I can break this into 3 different formulas that will all fire however would like to avoid that is possible
What about (\A|^)(12.0|8.0|18.0.1025.168)($|\z)

Filter by regex example

Could anyone provide an example of a regex filter for the Google Chrome Developer toolbar?
I especially need exclusion. I've tried many regexes, but somehow they don't seem to work:
It turned out that Google Chrome actually didn't support this until early 2015, see Google Code issue. With newer versions it works great, for example excluding everything that contains banners:
/^(?!.*?banners)/
It's possible -- at least in Chrome 58 Dev. You just need to wrap your regex with forward-slashes: /my-regex-string/
For example, this is one I'm currently using: /^(.(?!fallback font))+$/
It successfully filters out any messages that contain the substring "fallback font".
EDIT
Something else to note is that if you want to use the ^ (caret) symbol to search from the start of the log message, you have to first match the "fileName.js?someUrlParam:lineNumber " part of the string.
That is to say, the regex is matching against not just the log message, but also the stack-entry for the line which made the log.
So this is the regex I use to match all log messages where the actual message starts with "Dog":
/^.+?:[0-9]+ Dog/
The negative or exclusion case is much easier to write and think about when using the DevTool's native syntax. To provide the exclusion logic you need, simply use this:
-/app/ -/some\sother\sregex/
The "-" prior to the regex makes the result negative.
Your expression should not contain the forward slashes and /s, these are not needed for crafting a filter.
I believe your regex should finally read:
!(appl)
Depending on what exactly you want to filter.
The regex above will filter out all lines without the string "appl" in them.
edit: apparently exclusion is not supported?

Replacing all instances of a name in all strings in a solution

We have a large solution with many projects in it, and throughout the project in forms, messages, etc we have a reference to a company name. For years this company name has been the same, so it wasn't planned for it to change, but now it has.
The application is specific to one state in the US, so localizations/string resource files were never considered or used.
A quick Find All instances of the word pulled up 1309 lines, but we only need to change lines that actually end up being displayed to the user (button text, message text, etc).
Code can be refactored later to make it more readable when we have time to ensure nothing breaks, but for time being we're attempting to find all visible instances and replace them.
Is there any way to easily find these "instances"? Perhaps a type of Regex that can be used in the Find All functionality in Visual Studio to only pull out the word when it's wrapped inside quotes?
Before I go down the rabbit hole of trying to make my job easier and spending far more time than it would have taken to just go line by line, figured I would see if anyone has done something like this before and has a solution.
You can give this a try. (I hope your code is under source control!)
Foobar{[^"]*"([^"]*"[^"]*")*[^"]*}$
And replace with
NewFoobar\1
Explanation
Foobar the name you are searching for
[^"]*" a workaround for the missing non greedy modifier. [^"] means match anything but " that means this matches anything till the first ".
([^"]*"[^"]*")* To ensure that you are matching only inside quotes. This ensures that there are only complete sets of quotes following.
[^"]* ensures that there is no quote anymore till the end of the line $
{} the curly braces buts all this stuff following your companies name into a capturing group, you can refer to it using \1
The VS regex capability is quite stripped down. It perhaps represents 20% of what can be done with full-powered regular expressions. It won't be sufficient for your needs. For example, one way to solve this quote-delimited problem is to use non-greedy matching, which VS regex does not support.
If I were in your shoes, I would write a perl script or a C# assembly that runs outside of Visual Studio, and simply races through all files (having a particular file extension) and fixes everything. Then reload into Visual Studio, and you are done. Well, if all went well with the regex anway.
Ultimately what you really must watch out for is code like this:
Log.WriteLine("Hello " + m_CompanyName + " There");
In this case, regex will think that "m_CompanyName" appears between two quotes - but it is not what you meant. In this case you need even more sophistication, and I think you'll find the answer with a special .net regular expression extension.

Regex - match a string not contain a 'semi-word'

I tried to make regex syntax for that but I failed.
I have 2 variables
PlayerInfo[playerid][pLevel]
and
Character[playerid]
and I want to catch only the second variable,I mean only the world what don't contain PlayerInfo, but cointains [playerid]
"(\S+)\[playerid\]" cath both words and (\S+[^PlayerInfo])\[playerid\] jump on some variables- they contais p,l,a,y ...
I need to replace in notepad++,all variables like Text[playerid] to ExClass [playerid][Text]
Couple Pluasible solutions.
List item
Notepad has a plugin called python script. Running regex from there
gives full regex functionality, the python version anyway, and a lot
of powerful potential beyond that. And I use the online python regex tester to help out.
RegRexReplace plugin helps create regex plugins in Notepad++, so when you do hit a limitation, you find out a lot quicker.
Or of course default to your alternate editor (I'm assuming you have
one?) or this online regex tool is absolutely amazing. You
can perform the action on the text online as well.
(I'd try to build a regex for you, but I'm a bit lost as to what you're looking for. Unless the Ivo Abeloos got it. If you're still coming up short, maybe a code example along with values displayed?)
Good luck!
It seems that Notepad++ support negative lookbehind since v6.
In notepad++ you could try to replace (.+)\[(.+)\] with ExClass\[\2\]\[\1\]
Try to use negative lookbehind.
(?<!PlayerInfo)\[playerid\]
EDIT: unfortunately notepad++ does not support negative lookbehind.
I tried to make a workaround based on the following naive idea:
(.[^o]|[^f]o)[playerid]
But this expression does not work either. Notepad++ seems to fail in alternative operator. Thus the answer is: it is impossible to do exactly what you want. Try to solve the problem in other way or use alternative tool.