Regex.Matches提取成对引号中的字符串
使用正则表达式配合regex.matches方法可以一次性提取所有成对引号内的内容。调用时传入包含双引号的源字符串和匹配模式"([^"]*)",即可获取所有匹配项的捕获组值。该方法默认支持贪婪匹配,能准确处理嵌套或连续出现的引号片段。

using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
string input = "He said \"Hello\" and she replied \"Hi\".";
MatchCollection matches = Regex.Matches(input, @"\"([^\"]*)\"");
foreach (Match match in matches)
{
Console.WriteLine(match.Groups[1].Value);
}
}
}
运行上述代码将输出两行文本:第一行为Hello,第二行为Hi。这验证了正则表达式能够正确跳过非引号部分并提取目标子串。
String.Split按引号分割提取内容
通过String.Split方法以双引号为分隔符进行拆分,也可以间接提取引号内的内容。由于Split会移除分隔符本身,因此偶数索引位置的元素即为被引号包围的内容。这种方法适用于简单场景,但不支持复杂模式匹配。

Hello











