47

I want to extract only those words within double quotes. So, if the content is:

Would "you" like to have responses to your "questions" sent to you via email?

The answer must be

  1. you
  2. questions
4

8 回答 8

69

Try this regex:

\"[^\"]*\"

or

\".*?\"

explain :

[^ character_group ]

Negation: Matches any single character that is not in character_group.

*?

Matches the previous element zero or more times, but as few times as possible.

and a sample code:

foreach(Match match in Regex.Matches(inputString, "\"([^\"]*)\""))
    Console.WriteLine(match.ToString());

//or in LINQ
var result = from Match match in Regex.Matches(line, "\"([^\"]*)\"") 
             select match.ToString();
于 2012-10-23T05:28:40.400 回答
21

基于@Ria 的回答:

static void Main(string[] args)
{
    string str = "Would \"you\" like to have responses to your \"questions\" sent to you via email?";
    var reg = new Regex("\".*?\"");
    var matches = reg.Matches(str);
    foreach (var item in matches)
    {
        Console.WriteLine(item.ToString());
    }
}

输出是:

"you"
"questions"

如果不需要,可以使用 string.TrimStart() 和 string.TrimEnd() 删除双引号。

于 2012-10-23T05:40:58.713 回答
15

我喜欢正则表达式解决方案。你也可以想到这样的事情

string str = "Would \"you\" like to have responses to your \"questions\" sent to you via email?";
var stringArray = str.Split('"');

然后odd从数组中取出元素。如果你使用 linq,你可以这样做:

var stringArray = str.Split('"').Where((item, index) => index % 2 != 0);
于 2012-10-23T05:47:46.397 回答
5

这也会从@Ria 窃取正则表达式,但允许您将它们放入一个数组中,然后在其中删除引号:

strText = "Would \"you\" like to have responses to your \"questions\" sent to you via email?";
MatchCollection mc = Regex.Matches(strText, "\"([^\"]*)\"");
for (int z=0; z < mc.Count; z++)
{
    Response.Write(mc[z].ToString().Replace("\"", ""));
}
于 2013-11-27T03:37:10.727 回答
3

我结合了正则表达式和修剪:

const string searchString = "This is a \"search text\" and \"another text\" and not \"this text";
var collection = Regex.Matches(searchString, "\\\"(.*?)\\\"");
foreach (var item in collection)
{
    Console.WriteLine(item.ToString().Trim('"'));
}

结果:

search text
another text
于 2018-12-04T03:33:07.453 回答
1

Try this (\"\w+\")+

I suggest you to download Expresso

http://www.ultrapico.com/Expresso.htm

于 2012-10-23T05:27:16.053 回答
0

@ria 的回答略有改进,

\"[^\" ][^\"]*\"

仅当后面没有空格以允许尾随英寸说明符时,才会识别起始双引号。

在此处输入图像描述

副作用:它不会将 "" 识别为引用值。

于 2022-02-15T01:55:31.320 回答
0

我需要在 C# 中执行此操作来解析 CSV,但这些都不适合我,所以我想出了这个:

\s*(?:(?:(['"])(?<value>(?:\\\1|[^\1])*?)\1)|(?<value>[^'",]+?))\s*(?:,|$)

这将解析带或不带引号的字段,并将从值中排除引号,同时保留嵌入的引号和逗号。<value>包含解析的字段值。在不使用命名组的情况下,组 2 或组 3 包含该值。

有更好、更有效的方法来进行 CSV 解析,但这种方法在识别错误​​输入方面无效。但是,如果您可以确定您的输入格式和性能不是问题,那么这可能对您有用。

于 2017-10-29T00:56:57.853 回答