using regexp in visual studio 2010 to find and replace attributes - regex

i have a regexp (c|C)(lass)="([A-Za-z0-9_-]\s)(popup)(\s[A-Za-z0-9_-])" that will return the class attribute with a value of popup inside.
I am having difficulty to get this to run in the visual studio 2010 find and replace. Ultimately i would like to replace the popup with another class name lets say im-with-stupid
My first issue is with running the find returning Grouped expression is missing ')'. My assumption is the error is caused by the ". How can i escape this in the search.
Bonus points for the replace regexp - sorry i cant get that far to make an example code yet.

VS2010:
Find What:
{[cC]lass="[A-Za-z0-9_\-]+ }popup{ [A-Za-z0-9_\-]+"}
Replace with:
\1im-with-stupid\2
Find options:
Use Regular expressions
VS2012:
Find What:
([cC]lass="\w+\s)popup(\s\w+")
Replace with:
$1im-with-stupid$2
Find options:
Use Regular expressions

Related

Regex for form.controls['property']

I am trying to find replace thousands of strings in code - form.controls['property'] with form.get('property') where property is a variable.
So far I got find expression .controls\['\w+'\] with replace expression .get\('\w+'\) but VSCode is replacing .controls['products'] with .get\('\w+'\) for string productForm.controls['products'].controls.length. I need to replace it with productForm.get('products').controls.length Any ideas how to fix it? Thanks!
Parentheses () capture a group. Money sign $ accesses a group by index.
.controls\['(\w+)'\]
.get('$1')
This is the result in VS Code:
See also: What flavor of Regex does Visual Studio Code use?

Find and replace using regular expressions (regex) in visual studio?

I am converting from VB.net to C#.Net.
I need to replace Session("something") or Session("Others") to Session["something"] or Session["Others"] in the whole project. What regex i need to use in the find box and what's in the replace box. Any help would be appreciated.
Make sure you check on the option use the regular expression.
Find what: Session\("{.*?}"\)
Replace with: Session["\1"]
Note: using lazy operator ? here is try to stop when finding the first match. Thus, it should be able to handle cases like:
Session("Types") = new SelectList(xxx, "Code", "Description");
((((Session("something)))))))))"))))
P.S.: In VS2013, you should use the following instead:
Find what: Session\("(.*?)"\)
Replace with: Session["$1"]

To lower case using Find and Replace with regular expression in Visual Studio 2010

I am using the Find and Replace function in Visual Studio 2010 in order to change the coding style for fields.
All instances similar to
m_MyField
should be
_myField
but I can only manage to get
_MyField
using
Find what: m_{[a-zA-Z]}
Replace with: _\1
How do I make the first letter lower case? I have tried the following but it does not work:
Find what: m_{[a-zA-Z]}
Replace with: _\L\1
I found this explained in http://vim.wikia.com/wiki/Changing_case_with_regular_expressions but it only works for vim I think.
Any suggestions are welcomed.
I would use this approach:
Find what: m_A{[a-zA-Z]}
Replace with: _a\1
and repeat it for B..Z
Not elegant but should be done quick.

Visual Studio Find and Replace with Regex

I want to replace C# attributes with VB.NET, which means, [Serializable] should become <Serializable>.
The pattern (\[)(.+)(\]) does find the results but I don't know how to replace the first and the last groups with the appropriate parenthesis.
I read this page, but I didn't understand how to use the curly braces for F&R, I tried to wrap the groups with it but it didn't work.
If you are using the Productivity Power Tools extension from Microsoft that support normal .NET regexes, what you would put in the textbox for the replacement given your regular expression above is:
<$2>
where $2 refers to the second capture group in your regex, i.e. the text between the brackets.
Note that this only works with the Quick Find from Productivity Power Tools though. The normal find/replace in Visual Studio use another syntax altogether.
Find what: \[{Serializable}\]
Replace with: <\1>

Visual Studio 2008 search and replace regex

I have a large solution with a lot of lines that I need to replace.
In Visual Studio, you can search and replace with the aid of regular expressions.
I want to replace lines like:
rst.Fields("CustomerName").Value
rst.Fields("Address").Value
rst.Fields("Invoice").Value
To:
row("CustomerName").ToString()
row("Address").ToString()
row("Invoice").ToString()
Thus keeping the dynamic text part, which can vary.
Is this possible and how?
Update, solution:
Search: rst.Fields{\(.*\)}\.Value
Replace: rst\1.ToString()
Thanks JaredPar!
Try the following
Search Expression: ASpecificCommand(\(.*\))\.ASpecificProperty
Replace Expression: ATotallyDifferentCommand\1.ATotallyDifferentProperty
Note: This is not a perfect solution. Since there are (s involved and hence matching of nested parens, a regex won't ever be a perfect solution. However it should get the job done for the specific pattern you posted
The answer and solution provided helpful in doing a find-replace on messageboxes.
This worked in Visual Studio 2008 (VB .NET):
Example:
MessageBox.Show("Invalid Entry","Error")
Find What:
MessageBox.Show{(.*,*)}
Replace WIth:
Error.ShowError\1\2
Results in:
Error.ShowError("Invalid Entry","Error")
Looks like you have it nailed. It's what is called a "tagged expression" and you can see another example here:
http://blogs.msdn.com/b/zainnab/archive/2010/09/12/replace-in-files-tagged-expressions-vstipfind0016.aspx