简体   繁体   中英

C# Regex capturing parentheses

I am having trouble capturing parentheses.

I have a big file with data of this form:

I.u[12] = {n: "name1",...};
I.u[123] = {n: "name2",...};
I.u[1234] = {n: "name3",...};

I want to create a system which helps me get the name (here name1 , name2 , name3 ) out of the file if I provide the id (here 12 , 123 , 1234 ). I have the following code:

    public static string GetItemName(int id)
    {
        Regex regex = new Regex(@"^I.u\["+id+@"\]\s=\s{n:\s(.+),.+};$");
        Match m= GetMatch(regex,filepath);
        if(m.Success) return m.Groups[0].Value;
        else return "unavailable";
    }

    public static Match GetMatch(Regex regex, string filePath)
    {
        Match res = null;
        using (StreamReader r = new StreamReader(filePath))
        {
            string line;
            while ((line = r.ReadLine()) != null)
            {
                res = regex.Match(line);
                if (res.Success) break;
            }
        }
        return res;
    }

The regex finds the correct line in the file but I really don't know why it doesn't extract the name as I wanted and,

if(m.Success) return m.Groups[0].Value;

returns me the whole line in the file and not the name... I tried a lot of things, even changing m.Groups[0] to m.Groups[1] but it didn't work.

I have searched for a moment now without success. Would you have an idea of what is wrong?

Based on your updated question, I can see that you are using a greed quantifier: .+ . This will match as much as possible. You want a passive modifier, which will only match as much as necessary: .+?

Try this:

Regex regex = new Regex(@"^I.u\["+id+@"\]\s=\s\{n:\s(?<Name>.+?),.+\};$", RegexOptions.Multiline);

Then:

if(m.Success) return m.Groups["Name"].Value;

As others have pointed out, this:

if(m.Success) return m.Groups[0].Value;

Should be:

if(m.Success) return m.Groups[1].Value;

However, this will return "name1" including the quotes. Try and amend your regex pattern to:

@"^I.u\["+id+@"\]\s=\s{n:\s""(.+)"",.+};$"

which will exclude the quotes from m.Groups[1].Value

Because you are referring to wrong group number..It should be 1 not 0

Group 0 would always contain the whole match regardless of how many groups you have..

Also the regex should be

^I.u\["+id+@"\]\s*=\s*{n:\s*""(.+)"",.+};$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM