繁体   English   中英

使用正则表达式在引号内获取文本

[英]get text inside quotes using regex

我想使用正则表达式提取引号内的数据

My Text is : boundary="s323sd2342423---"

现在我需要在不使用子字符串的情况下提取双引号内的值。

我尝试了以下但没有帮助。

String pattern = @"boundary=""(?<value>[^""]*";
Match m = Regex.Match(rawMessage, pattern);
while (m.Success)
{
    boundaryString = m.Groups["value"].Value;
    m = m.NextMatch();
}

您需要关闭组的左括号

String pattern = @"boundary=""(?<value>[^""]*)";

现在,如果你去

Console.WriteLine(m.Groups["value"].Value);

将打印:

s323sd2342423---

您可以使用此模式,它将起作用。

String pattern = @"boundary=\""(?<value>.+?)\""";

使用以下正则表达式,无需任何分组即可获得所需内容

(?<=boundary=")[^"]+(?=")

获取引用文本的代码:

string txt = "boundary=\"s323sd2342423---\"";
string quotedTxt = Regex.Match(txt, @"(?<=boundary="")[^""]+(?="")").Value;

根据你的详细信息:

我想使用正则表达式提取引号内的数据

为什么不使用这种模式:

"(?<value>[^"]+)"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM