正则表达式:C# 提取双引号内的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13024073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:23:31  来源:igfitidea点击:

Regex: C# extract text within double quotes

c#regex

提问by new_linux_user

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
  1. 问题

回答by opewix

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

尝试这个 (\"\w+\")+

I suggest you to download Expresso

我建议你下载 Expresso

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

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

回答by Ria

Try this regex:

试试这个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.

[^ character_group ]

否定:匹配任何不在 character_group 中的单个字符。

*?

匹配前一个元素零次或多次,但尽可能少。

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();

回答by Edi Wang

Based on @Ria 's answer:

基于@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());
    }
}

The output is:

输出是:

"you"
"questions"

You can use string.TrimStart() and string.TrimEnd() to remove double quotes if you don't want it.

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

回答by bart s

I like the regex solutions. You could also think of something like this

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

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

Then take the oddelements from the array. If you use linq, you can do it like this:

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

var stringArray = str.Split('"').Where((item, index) => index % 2 != 0);

回答by vapcguy

This also steals the Regex from @Ria, but allows you to get them into an array where you then remove the quotes:

这也从@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("\"", ""));
}

回答by sthames42

I needed to do this in C# for parsing CSV and none of these worked for me so I came up with this:

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

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

This will parse out a field with or without quotes and will exclude the quotes from the value while keeping embedded quotes and commas. <value>contains the parsed field value. Without using named groups, either group 2 or 3 contains the value.

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

There are better and more efficient ways to do CSV parsing and this one will not be effective at identifying bad input. But if you can be sure of your input format and performance is not an issue, this might work for you.

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

回答by Jared Chu

I combine Regex and Trim:

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

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('"'));
}

Result:

结果:

search text
another text