简体   繁体   English

C#正则表达式不返回

[英]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 我需要将字符串从引号中拉出(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. 我尝试了几种引用.Value方法,但是我也无法记录普通字符串,因此我的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. Regex101告诉我应该匹配得很好,所以我的C#实现中必须有一些错误。 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 regex = new Regex("\"(.+)\"");

Result: 结果:

"BOB" “ BOB”

string: "BOB" and match value: BOB 字符串:“ BOB”,匹配值:BOB

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

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