空格和单词的正则表达式

时间:2012-01-09 14:29:41

标签: c#

有以下文本文件:

<div>
    <asp:HyperLinkField HeaderText="Hello World of Regular Expression" />
</div>

如何获得任何单词和空格的双引号之间的内容?

更新

//This one gets me close but doesn't get me strings with spaces in them
var match = Regex.Match(tokens[1], @"HeaderText=\""(\w+)\"""); 
//This was suggested below. It shows correct match count but values are just empty strings
var match = Regex.Match(tokens[1], @"HeaderText=""[^""]+""|[\w]+");

if (match.Success)
{
    yield return new KeyValuePair<string, string>(
        file, match.Groups[1].Value //This is empty for 2nd scenario
    );
}

1 个答案:

答案 0 :(得分:2)

试试这个:

"[^"]+"|[\w]+

这将返回匹配列表,单个词和引号之间的整个表达式。