简体   繁体   中英

Extract data from a string using Regex.Matches

I have a string that always takes a general form. I wish to extract information from it and place it in an array.

Given the following input:
John Doe +22\\r\\nPong

I want the following output
John Doe
+22
Pong

I'm using the following bit of code to extract the details I want.

public static string[] DetailExtractor(string input)
        {
            return Regex.Matches(input, @"(.*(?=\s\+))|(\+\d{1,2}(?=\\r\\n))|((?<=\\r\\n).*)")
                 .OfType<Match>()
                 .Select(m => m.Value)
                 .ToArray();
        }

But it gives me the following output:
Player Name
""

However, using the same regex expression in this online regex tester matches all the elements I want.

Why does it work for one and not the other? Does Regex.Matches not work the way I think it does?

You can try with one of these:

[a-z]+ [a-z]+ \+[0-9]{1,}\\r\\n[a-z]+

or:

[a-z\s\\]+\+[0-9]{1,}[a-z\s\\]+

or:

[\w\s]+\+\d{1,}\\r\\n[\w]+

Just taking a guess here, but I'm betting that you are using the following:

var details = DetailExtractor("John Doe +22\\r\\nPong");

The above would convert \\r\\n to the a carriage return and a new line character. This would prevent the regex you wrote from working. Instead you can specify a raw string in C# or escape the \\r\\n :

var details = DetailExtractor(@"John Doe +22\\r\\nPong");

or

var details = DetailExtractor("John Doe +22\\\\r\\\\nPong");

As everyone else has pointed out there's simpler regexes available to do the same type of matching depending on your needs.

The regex below is slightly simpler, but the string array return is slightly more complex.

public static string[] DetailExtractor1(string input)
{
    var match = Regex.Match(input, @"^(?<name>\w+\s+\w+)\s+(?<num>\+\d+)\r\n(?<type>\w+)");

    if (match.Success)
    {
        return new string[] {
            match.Groups["name"].Value,
            match.Groups["num"].Value,
            match.Groups["type"].Value
        };
    }

    return null;
}

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