Reputation: 1758
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!
Upvotes: 0
Views: 207
Reputation: 14153
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
Upvotes: 3