简体   繁体   English

C#正则表达式捕获括号

[英]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 ). 我想创建一个系统,它可以帮助我得到的名称(这里name1name2name3出来的文件),如果我提供的ID(这里121231234 )。 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. 返回我文件中的整行而不是名称...我尝试了很多事情,甚至将m.Groups[0]更改为m.Groups[1]但没有用。

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. 但是,这将返回"name1"包括引号。 Try and amend your regex pattern to: 尝试将您的正则表达式模式修改为:

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

which will exclude the quotes from m.Groups[1].Value 这将从m.Groups[1].Value排除引号

Because you are referring to wrong group number..It should be 1 not 0 因为您指的是错误的组号。它应该是1而不是0

Group 0 would always contain the whole match regardless of how many groups you have.. 不论您有多少组,组0始终包含整个比赛。

Also the regex should be 正则表达式也应该是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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