C# 在两个模式之间抓取文本

C# 在两个模式之间抓取文本,c#,C#,检查下面的代码。我想抓住这个id=“a-popover-sp-info-popover-之间的所有内容,直到”。我已经尝试使用下面的Regex.Match公式,但出现语法错误。它在c#中无效。我怎样才能以正确的方式做到这一点。我的目标是抓取ABC123文本 string foo = @id="a-popover-sp-info-popover-ABC123"; string output = Regex.Match(foo, @"id="a-popove

检查下面的代码。我想抓住这个
id=“a-popover-sp-info-popover-
之间的所有内容,直到
。我已经尝试使用下面的
Regex.Match
公式,但出现语法错误。它在c#中无效。我怎样才能以正确的方式做到这一点。我的目标是抓取
ABC123
文本

string foo = @id="a-popover-sp-info-popover-ABC123";

string output = Regex.Match(foo, @"id="a-popover-sp-info-popover-(.*)"").Groups[1].Value;

我只需要抓取文本:
ABC123

您必须确保用引号将字符串括起来”。 如果要在字符串中包含引号,则必须使用反斜杠将其转义:

string foo = "id=\"a-popover-sp-info-popover-ABC123\"";

string output = Regex.Match(foo, "id=\"a-popover-sp-info-popover-(.*)\"").Groups[1].Value;

由于您的模式非常严格,实际上string.Split方法也可以做到这一点:

string output1 = foo.Split(new string[] {"info-popover-"}, StringSplitOptions.RemoveEmptyEntries)
                    .Last()
                    .TrimEnd('"');
Console.WriteLine(output1);
输出:

ABC123

string pattern=“id=\”a-popover-sp-info-popover-[a-Z]{3}[1-9]{3}\”;
string input=“id=\”a-popover-sp-info-popover-ABC123\”;
Match m=Regex.Match(输入,模式);
if(m.Success)Console.WriteLine(“找到”{0}',m.Value);

你定义
foo
的方式是无效的。你的意思是
string foo=“@id=\”a-popover-sp-info-popover-ABC123\”
?@PetervanderHeijden我想这是John说的意思:“但是有语法错误。它在c#中无效。“@PetervanderHeijden是的,现在foo在c#中有效。但是我如何在
Regex.Match
中使用它呢?我想知道您是否需要Regex。似乎您只需要
“a-popover-sp-info-popover”之后的剩余字符串-“
这也可以使用拆分完成。从你的帖子来看,这个模式似乎是固定的,而且总是同一个词。