0

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!

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.