【问题标题】:Regex: C# extract text within double quotes正则表达式:C# 提取双引号内的文本
【发布时间】:2012-10-23 05:25:34
【问题描述】:

我只想提取双引号内的那些单词。所以,如果内容是:

“您”是否希望通过电子邮件将您的“问题”回复发送给您?

答案一定是

  1. 问题

【问题讨论】:

  • 您是否期望在引用的字符串中出现转义引号?如I am "5'7\"" tall?

标签: c# regex


【解决方案1】:

试试这个regex:

\"[^\"]*\"

\".*?\"

解释:

[^ character_group ]

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

*?

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

和一个示例代码:

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

【讨论】:

  • 就像旁注一样:如果你使用带有前导@的字符串,你可以写@"""([^""]*)"""。这对于显示的示例并不重要,但对于具有其他模式的其他用户来说可能很重要。当您编写像@"..." 这样的字符串时,您不需要转义反斜杠字符。 (例如,当您使用许多反斜杠符号(如 \d\w 等)时很有用)在这种情况下(带有前导 @),您将引号字符转义为 "" 而不是 \"。在现代 Visual Studio 版本中,转义引号以其他颜色显示,以便更好地进行概览。
【解决方案2】:

根据@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() 删除双引号。

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

        我将 Regex 和 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('"'));
        }
        

        结果:

        search text
        another text
        

        【讨论】:

          【解决方案6】:

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

          建议你下载Expresso

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

          【讨论】:

            【解决方案7】:

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

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

            这将解析带或不带引号的字段,并从值中排除引号,同时保留嵌入的引号和逗号。 &lt;value&gt; 包含解析的字段值。如果不使用命名组,组 2 或组 3 中的任何一个都包含该值。

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

            【讨论】:

              【解决方案8】:

              @ria 的回答略有改进,

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

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

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

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2014-09-14
                • 1970-01-01
                • 2013-05-10
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2012-03-24
                相关资源
                最近更新 更多