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
- you
- questions
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();
基于@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() 删除双引号。
我喜欢正则表达式解决方案。你也可以想到这样的事情
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);
这也会从@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("\"", ""));
}
我结合了正则表达式和修剪:
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
我需要在 C# 中执行此操作来解析 CSV,但这些都不适合我,所以我想出了这个:
\s*(?:(?:(['"])(?<value>(?:\\\1|[^\1])*?)\1)|(?<value>[^'",]+?))\s*(?:,|$)
这将解析带或不带引号的字段,并将从值中排除引号,同时保留嵌入的引号和逗号。<value>
包含解析的字段值。在不使用命名组的情况下,组 2 或组 3 包含该值。
有更好、更有效的方法来进行 CSV 解析,但这种方法在识别错误输入方面无效。但是,如果您可以确定您的输入格式和性能不是问题,那么这可能对您有用。