Regex.Matches C#双引号 [英] Regex.Matches c# double quotes

查看:103
本文介绍了Regex.Matches C#双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了下面这个code适用于单引号。 它发现了单引号之间的所有的话。 但我将如何修改正则表达式用双引号工作?

关键词是从一种形式后未来

所以

 关键字='和平这个世界是,然后一些'


    //匹配所有引述领域
    MatchCollection COL = Regex.Matches(关键字,@'(*)。?');

    //复制组到一个String []数组
    字符串[]字段=新的字符串[col.Count]
    的for(int i = 0; I< fields.Length;我++)
    {
        域[我] =山坳[I] .Groups [1] .value的; //(索引1是第一组)
    } //匹配所有引述领域
    MatchCollection COL = Regex.Matches(关键字,@'(*)。?');

    //复制组到一个String []数组
    字符串[]字段=新的字符串[col.Count]
    的for(int i = 0; I< fields.Length;我++)
    {
        域[我] =山坳[I] .Groups [1] .value的; //(索引1是第一组)
    }
 

解决方案

您将只需更换 \键,删除文字适当重建它。

  MatchCollection COL = Regex.Matches(?关键词,\\\(*)\\\);
 

I got this code below that works for single quotes. it finds all the words between the single quotes. but how would I modify the regex to work with double quotes?

keywords is coming from a form post

so

keywords = 'peace "this world" would be "and then" some'


    // Match all quoted fields
    MatchCollection col = Regex.Matches(keywords, @"'(.*?)'");

    // Copy groups to a string[] array
    string[] fields = new string[col.Count];
    for (int i = 0; i < fields.Length; i++)
    {
        fields[i] = col[i].Groups[1].Value; // (Index 1 is the first group)
    }// Match all quoted fields
    MatchCollection col = Regex.Matches(keywords, @"'(.*?)'");

    // Copy groups to a string[] array
    string[] fields = new string[col.Count];
    for (int i = 0; i < fields.Length; i++)
    {
        fields[i] = col[i].Groups[1].Value; // (Index 1 is the first group)
    }

解决方案

You would simply replace the ' with \" and remove the literal to reconstruct it properly.

MatchCollection col = Regex.Matches(keywords, "\\\"(.*?)\\\"");

这篇关于Regex.Matches C#双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆