Dimitar Tsonev
Dimitar Tsonev

Reputation: 3892

Replace string with regex and keep some of its content

In my code, I have statements like this:

 Project.MVC.Bll.Resources.<resource-name>

And I want to replace them with

 ProjectResources.GetString("<resouce-name>")

I want to use the visual studio Find And Replace with regular expression, but the problem is that I don't know how to "take" the resource-name.

For the search pattern I'm using Project.MVC.Bll.Resources.* , but I have no idea what the replacement pattern will be, in order to put the resource name in the brackets.

Upvotes: 0

Views: 1040

Answers (1)

Roman
Roman

Reputation: 20256

The construct you're looking for is called captures (or sometimes capture groups). There's an example of how to do that here. There also looks to be an MSDN page on exactly this topic: Using Regular Expressions in Visual Studio with an example of using numbered captures.

Also worth pointing out that earlier versions of Visual Studio used slightly different syntax for this.

In your case the capture will probably look something like

Project\.MVC\.Bll\.Resources\.(?<name>____) 

and the replacement along the lines of

ProjectResources.GetString("${name}")

Just replace ____ with whatever pattern your resource names follow. Also don't forget that . means any character in RegEx, so you need to escape them to only match periods.

Upvotes: 3

Related Questions