Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2017 - Find and replace with text

I have this property in a class:

public string fooo { get; set; }

I need to rewrite it in something like that :

[BsonElement("fooo")] 
public string fooo{ get; set; }

I have to do it for a LOT of properties, so I would like to use the 'Find a replace feature' in Visual Studio.

I use the following macro :

Search : public 
Replace : [BsonElement("")]\n\t\tpublic 

How is it possible using regex to get dynamically the fooo name and put it in the BsonElement property ? And globally, is it possible to have a link to documentation about this, haven't find it.

like image 778
Xavier W. Avatar asked Feb 11 '26 18:02

Xavier W.


2 Answers

Here's a solution for properties formatted like public string fooo { get; set; } (whitespace is important: one-liners, with the space after property name)

Search:

(public) (\w+) (\w+) { get; set; }

Replace:

[BsonElement("$3")]\n\t\t$1 $2 $3 { get; set; }

Explanation:

$1, $2, $3 are groups captured in ()

\w is equivalent to [a-zA-Z0-9_], so (\w+) will match any type name and property name

like image 154
j4nw Avatar answered Feb 13 '26 08:02

j4nw


Find: (\s*)public string (\w+) { get; set; }

Replace: $1[BsonElement("$2")]\n$1public string $2{ get; set; }

This solution preserves the whitespace (tabs, etc) before the definition

Image for your convenience

like image 36
JoshuaKrstic Avatar answered Feb 13 '26 10:02

JoshuaKrstic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!