user3201077
user3201077

Reputation: 91

How to remove the specific characters using regex

How will i solve this? please don't mind the text of data annotation, this is for testing only. How will i remove the data annotation?

   [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)

I have this code

    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;
    }

but it gives me a wrong result.

       string name)

What i want is.

      int Get1Number([FromBody]string name)

Upvotes: 2

Views: 125

Answers (5)

Dzienny
Dzienny

Reputation: 3417

The following code will extract the int Get1Number([FromBody]string name) to the text variable.

var input = @" [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)";
var match = Regex.Match(input, @"\w+\s+\w+\(\[FromBody\]\w+\s+\w+\)");
var text = match.Groups[0].Value;

Upvotes: 1

Sky Fang
Sky Fang

Reputation: 1101

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

Upvotes: 1

NeverHopeless
NeverHopeless

Reputation: 11233

Try using this regex, it will eliminate the suppress messages and also support for other access modifiers too:

(\[[^]]+\)\])(?<modifiers>\s+\w+\s)?

If you don't want to remove access modifiers then simply skip this named group during Regex.Replace.

Demo

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626689

You can try performing this regex replacement:

(?ims)^\s*\[(?!\bpublic\b)[\s\S]*?\)\]\s*public\s*

Replace with an empty string.

C#:

var result = Regex.Replace(str, @"(?ims)^\s*\[(?!\bpublic\b)[\s\S]*?\)\]\s*public\s*", string.Empty);

Here is a demo

Upvotes: 1

garyh
garyh

Reputation: 2852

Try this

Regex.Replace(inputStr, @"(?<!\()\[[^\]]+\]\r\n", string.Empty));

It matches a text enclosed in [] but not where they are preceded by a (

Upvotes: 1

Related Questions