Regex to replace parentheses with square bracket - regex

I need to convert some variables that use parentheses instead of bracket in a Javascript file. For example, MyVariable(i) should be MyVariable[i].
I use the Find and Replace tool of Visual Studio with this Regex :
MyVariable\({(.+)}\)
and replace with:
MyVariable\[\1\]
This work fine for case like :
asdas.MyVariable(i+1)
asdas.MyVariable(i)
asdas.MyVariable(i).asd
MyVariable(i+1)
But doesn't work for case like
if (parseInt(OtherObject.MyVariable(i+1).Dest.XYZ)==SINK_STATE_TYPE || OtherObject.MyVariable(i).X == "ok")
The last one will do take the first parentheses and the last one of the line and act weird. What do I need to change in the Regex to be able to make it works with line that has multiple parentheses.

Your pattern is greedy. The (.+) part matches everything, including a closing parenthesis ) character until it reaches the last ) character. To make it non-greedy you can use # instead of +, which means "match one or more occurrences of the preceding expression, and match as few characters as possible." In .NET this is usually handled by placing a ? after it, such as .+? but the Visual Studio regex flavor is different and uses the # metacharacter instead.
The updated pattern becomes:
MyVariable\({(.#)}\)
You can also use [^)]+, which matches any character that is not a ) character. When it encounters a ) it will stop matching. This alternate pattern is:
MyVariable\({([^)]+)}\)
If you're familiar with the .NET regex flavor you might be interested in checking out the Visual Studio 2010 Productivity Power Tools, which has extended Find/Replace to allow the .NET regex flavor to be used.

Related

Regex in Visual Studio Comunity

I'm trying to replace a code using Regex in visual studio community, but I'm not getting it.
ZeroMemory(&ab,15);
ZeroMemory(&abc,30);
ZeroMemory(&tickt,sizeof(tickt));
To replace
memset(&ab,0,15);
memset(&abc,0,30);
memset(&tickt,0,sizeof(tickt));
I need to replace zeromemory with memset and the first one with ,0,
thank you all in advance
Clearly you are asking about the "Find and Replace" feature in Visual Studio.
As a starting point, something like this should do what you want:
From: ZeroMemory\s*\((.*),
To: memset($1, 0,
Because parentheses are used in regular expressions for matching groups, you need to escape them (\() if you want to match an actual parenthesis.
Then we use (.*) to create a group that captures whatever your first parameter is. Note that this is very naive. If you have a non-trivial expression for that argument (e.g. a function call where a comma appears anywhere) this will likely fail.
So that matches the stuff you want, and then for the replacement you can use $1 to substitute the first matching group (in which we captured your first argument).
The only extra detail I added was a \s* between the function identifier and the parenthesis. This matches optional whitespace characters, so it will still match even if you have stuff like:
ZeroMemory (foo, bar);
I recommend you read the documentation to familiarize yourself with regular expressions in Visual Studio.

Problem with regular expression using look behind feature

I try to build simple regular expression to remove some parts of bad (unwanted) code and needed use look behind feature.
It worked until i added \s+ to it to exclude spaces from mark.
Eliminating parts of expression i finally got to (?<=\s+)foo which is still warned as invalid expression.
It may looks a little weird or unclear so expanding it:
(?<=foo\s+)bar is warned as invalid expression, where (?<=foo)\s+bar is working but it marks spaces before bar.
I am use it in notepad++.
From the comment by #Toto Notepad++ does not support variable length lookbehind. It uses the boost regex.
Notepad++ does support \K to reset the starting point of the reported match.
\bfoo\s+\Kbar\b
Regex demo
Another way is to capture bar in a capturing group.
\bfoo\s+(bar)\b
Regex demo
Note that \s also matches a newline, and perhaps you might also use \h+ to match 1+ horizontal whitespace characters.

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

Find and replace string within a specific string with regex in visual studio

I'd like to replace automatically all the strings in my solution which are like this one
NotifyPropertyChanged("VariableParameter")
with this
NotifyPropertyChanged(Function() VariableParameter)
by "Quick Replace" in "Find and Replace" using a regular expression in Visual Studio 2010.
I have not the slightest idea how to do this when I have to keep each different variable parameter.
Try the following pattern and replacement.
Pattern: NotifyPropertyChanged\("{[^"]+}"\)
This matches your text, while escaping the parentheses. The {[^"]+} portion tags the contents (via the curly braces) and the [^"]+ bit matches any character that isn't a double-quote, one or more times.
Replacement: NotifyPropertyChanged(Function() \1)
This replaces the matched text and is fairly straightforward to understand. The \1 portion refers to the first (and only, in this example) tagged text from the pattern, which is the content between double-quotes.

How to replace . in patterned strings with / in Visual Studio

I have lot of code in our solution like this:
Localization.Current.GetString("abc.def.gih.klm");
I want to replace it with:
Localization.Current.GetString("/abc/def/gih/klm");
the number of dots (.) is variable.
How can I do this in Visual Studio (2010)?
Edit: I want to replace strings in code (in VS 2010 editor), not when I run my application
Thank you very much
Misread your request.
If you press ctrl+shift h and put this as your find string
{Localization\.Current\.GetString\("[A-Za-z\/]+}(\.)
Then put this as your replace with:
\1/
And then in find options tick use regular expressions.
This will find the first dot and replace it. Clicking find next will get the second one etc. You will have to keep doing a replace all until they are all done. Someone can probably improve that!
As shown below
Try this in the "Replace in Files" Dialogue with "Use Regular expressions"
Find what:
{[^"]*"[^"]*}\.
If you want to be a bit more strict on the allowed characters between the quotes then try this
{[^"]*"[A-Za-z.]*}\.
this would allow only ASCII characters and dots between the quotes.
Replace with
\1/
It will find the first " in a row and replace the last dot before the next " with /
The problem is, it replaces only the last occurrence of a dot within the first set of "" in each row. So you would have to call this a few times until you get the message "The text was not found"
And be careful if there is a wanted dot between "". it will be replaced also.
EDIT
you can't use this in visual studio as it has its own flavour of regex, not the one used in the .NET regex classes, and I don't think you can do lookbehind with it.
you can use this regex:
(?<=\("[\w.]+)\.
in the find and replace, replacing by .
Breaking it down:
Match a dot (the . at the end)
Which is preceeded by (positive look behind) a bracket ( followed by a " and then any number of characters which are letters or a dot (dots don't need to be escaped in a group)
if you are sure that the text that you want to replace only ever has the Localization.Current.GetString bit then you could include that in the lookbehind of the regex:
(?<=Localization\.Current\.GetString\("[\w.]+)\.