简体   繁体   中英

C# Regex not returning

I'm working on a practice problem which gives me a much longer set of strings. I need to pull the strings out of the quotation marks (wh

// names is a string[] composed of names like these: "BOB", "JOE", "FRANK", etc... 
// (the ""s are part of the string, not string designations). I suppose that makes them "\"Bob\"", etc...

foreach(string name in names)
{
    Regex regex = new Regex("/\"(.+)\"/");
    Match match = regex.Match(name);
    Console.WriteLine (match.Value);
    if (match.Success) 
    {
            Console.WriteLine("string: {0} and match value: {1}", name, match.Groups[1].Value);
    }
}

I'm not logging out anything. I've tried referencing .Value several ways, but I can't log normal strings either so I'm not getting any matches off of my Regex. I've followed a couple of examples too.

Regex101 tells me that I should be matching fine, so I've got to have some error in my C# implementation. But I can't figure it out. I just need someone to set me straight. Thanks!

Remove the forward slashes in your regex. They are used to indicate the start and end of a regular expression in some languages or formats, which is not needed when creating one through the Regex class.

Regex regex = new Regex("\"(.+)\"");

Result:

"BOB"

string: "BOB" and match value: BOB

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