我将如何解决这个问题?请不要介意数据注释的文本,这仅用于测试。我将如何删除数据注释?
[SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters",
MessageId = "YouSource.DI.Web.Areas.HelpPage.TextSample.#ctor(System.String)",
Justification = "End users may choose to merge this string with existing localized resources.")]
[SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly",
MessageId = "bsonspec",
Justification = "Part of a URI.")]
public int Get1Number([FromBody]string name)
我有这段代码
public void RemovePublicModifier()
{
this.method = this.SanitizeString(this.method, Public);
}
public void RemoveDataAnnotation()
{
int startIndex = this.method.LastIndexOf(']');
var result = this.method.Substring(startIndex + 1).Trim();
this.method = result;
}
private string SanitizeString(string method, string filter)
{
var items = method.Split(' ').ToList();
var publicItem = items.Where(i => i == filter).FirstOrDefault();
if (publicItem != null)
{
items.Remove(publicItem);
}
var result = string.Join(" ", items.ToArray()).Trim();
return result;
}
但它给我一个错误的结果。
string name)
我想要的是什么。
int Get1Number([FromBody]string name)
答案 0 :(得分:1)
string inputStr = @"[SuppressMessage(""Microsoft.Globalization"", ""CA1303:Do not pass literals as localized parameters"",
MessageId = ""YouSource.DI.Web.Areas.HelpPage.TextSample.#ctor(System.String)"",
Justification = ""End users may choose to merge this string with existing localized resources."")]
[SuppressMessage(""Microsoft.Naming"", ""CA2204:Literals should be spelled correctly"",
MessageId = ""bsonspec"",
Justification = ""Part of a URI."")]
public int Get1Number([FromBody]string name)";
Console.WriteLine(Regex.Replace(inputStr, @"(^|\s*)\[[\s\S]*?\]\s*public\s+", string.Empty));
public is also removed
答案 1 :(得分:1)
您可以尝试执行此正则表达式替换:
(?ims)^\s*\[(?!\bpublic\b)[\s\S]*?\)\]\s*public\s*
替换为空字符串。
C#:
var result = Regex.Replace(str, @"(?ims)^\s*\[(?!\bpublic\b)[\s\S]*?\)\]\s*public\s*", string.Empty);
这是demo
答案 2 :(得分:1)
试试这个
Regex.Replace(inputStr, @"(?<!\()\[[^\]]+\]\r\n", string.Empty));
它与[]
中的文字相匹配,但不会与(
答案 3 :(得分:1)
尝试使用此正则表达式,它将消除抑制消息,也支持其他访问修饰符:
(\[[^]]+\)\])(?<modifiers>\s+\w+\s)?
如果您不想删除访问修饰符,则只需在Regex.Replace
期间跳过此命名组。
答案 4 :(得分:1)
以下代码会将<div style="top: calc(35%);"></div>
提取到int Get1Number([FromBody]string name)
变量。
text