Ria*_*Ria 57
试试这个regex:
\"[^\"]*\"
Run Code Online (Sandbox Code Playgroud)
要么
\".*?\"
Run Code Online (Sandbox Code Playgroud)
解释:
[^ 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();
Run Code Online (Sandbox Code Playgroud)
Edi*_*ang 15
基于@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());
}
}
Run Code Online (Sandbox Code Playgroud)
输出是:
"you"
"questions"
Run Code Online (Sandbox Code Playgroud)
如果不需要,可以使用string.TrimStart()和string.TrimEnd()来删除双引号.
我喜欢正则表达式解决方案.你也可以想到这样的事情
string str = "Would \"you\" like to have responses to your \"questions\" sent to you via email?";
var stringArray = str.Split('"');
Run Code Online (Sandbox Code Playgroud)
然后odd从数组中获取元素.如果你使用linq,你可以这样做:
var stringArray = str.Split('"').Where((item, index) => index % 2 != 0);
Run Code Online (Sandbox Code Playgroud)
这也从 @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("\"", ""));
}
Run Code Online (Sandbox Code Playgroud)