Visual Studio搜索查找/替换中的Regex否定



我正在为Visual Studios编写一个find-and-replace,并试图将我们所有的公共变量切换为get/sets。

Regex: ^public ((?!({))(?!=).)*;

//MATCHES CORRECTLY
public string Country;
public string EmailAddress;
public string FirstName;
public string LastName;
public string PhoneNumber;
public string PostalCode;
public string State;
public List<Object> Example;
//MATCHES BUT SHOULDNT
public event GetUserListCompletedEventHandler GetUserListCompleted;
public delegate void GetUserListCompletedEventHandler(object sender, GetUserListCompletedEventArgs e);
//DOES NOT MATCH CORRECTLY
throw new Exception("In order to get the public date for blah blah blah");
public List<Order> orderDetails = new List<Order>();        
public string CustomerName { get; set; }
public string BillingAddress1 { get; set; }
public string BillingAddress2 { get; set; }

我已经能够在正则表达式中忽略事件/委托的内容,但在Visual Studio中,当我尝试使用代码时,正则表达式解析器会出现问题

^public ((?!({|event|delegate))(?!=).)*;

然而,这在VS 2019中不起作用,尽管根据他们自己的文件,|的使用是有效的。

https://learn.microsoft.com/en-us/visualstudio/ide/using-regular-expressions-in-visual-studio?view=vs-2019年

第二个问题是我正在做的输出$0{get;set;}

public string Country; { get; set; }

因此,我必须进行第二次查找/替换以将其更改为删除;,因为我不知道如何将分号之前的部分分组,同时不获取负匹配的匹配项。


所以我有两个问题

1( 如何在VS 中使用regex来忽略事件/委派

2( 如何让第一节的组输出,所以我忽略;在替换中。

非常感谢任何帮助或指导。

这个正则表达式怎么样。它忽略已替换的代码,也忽略初始化。它也没有捕获末尾的分号,而是要求它在那里。正如评论中所建议的,用第一个捕获组替换。我建议它是"$1{get;set;}"(可能因正则表达式引擎而异,我不熟悉c#引擎(

^(public (?!delegate|event)[^=n{]*(?=;))

https://regex101.com/r/VHwmeR/1

最新更新