正则表达式提取双引号之间的任何字符串(带双引号)

时间:2018-12-06 14:35:22

标签: regex

我不知道如何提取双引号之间的任何字符串,该字符串可能包含双引号,例如:

XSSFWorkbook workbook;

using (FileStream fs = new FileStream(@"c:\temp\test.xlsx", FileMode.Open, FileAccess.Read))
{
    workbook = new XSSFWorkbook(fs);
}

XSSFSheet sheet = workbook.GetSheetAt(0) as XSSFSheet;
sheet.CopyTo(workbook, $"{sheet.SheetName}_copy", true, true);

using (FileStream fs = new FileStream(@"c:\temp\test.xlsx", FileMode.Create, FileAccess.Write))
{
    workbook.Write(fs);
}

我尝试了""aa" => "aa "aa" => aa "" => "a\nb" => a\nb "\u0082" => \u0082 "ᅚ竁퇈" => ᅚ竁퇈 ,但在““ aa” =>“ aa

上失败了

1 个答案:

答案 0 :(得分:1)

您的示例“ a”无法处理,因为您不知道是否为null或“ aa”。

无论如何,如果您的样本在一行上,则可以使用正则表达式的开头和结尾:

sed 's/^"//;s/"$//'

测试:

$ cat file
""aa"
"aa"
""
"a\nb"
"\u0082"
"ᅚ竁퇈"

$ cat file | sed 's/^"//;s/"$//'
"aa
aa

a\nb
\u0082
ᅚ竁퇈