-2

文字遵循这种模式

<tr class="text" (any sequence of characters here, except ABC)ABC(any sequence of characters here, except ABC)
<tr class="text" (any sequence of characters here, except ABC)ABC(any sequence of characters here, except ABC)
<tr class="text" (any sequence of characters here, except ABC)ABC(any sequence of characters here, except ABC)
<tr class="text" (any sequence of characters here, except ABC)ABC(any sequence of characters here, except ABC)

所以基本上上面的行(可能包括换行符)可能会重复多次,这个想法是在 ABC 之后立即检索前 3 个字符。

我已经尝试过正则表达式

 \<tr class="text" [.\n]+ABC(?<capture>[.]{3})

但他们都失败了。有人可以给我一个提示吗?

4

3 回答 3

1

您有效地逃脱了通配符成为文字句点。只需使用

\<tr class="text" .+?ABC(?<capture>.{3})

确保您使用RegexOptions.Singleline, 以便也.匹配换行符!

但是,您实际上根本不应该使用正则表达式。相反,使用 DOM 解析器。我已经看到HTML Agility Pack经常被推荐用于 .NET。

于 2012-11-21T23:00:18.917 回答
0

"ABC"这是一个正则表达式,它将在您的字符串中捕获前 3 个字母

".+ABC(...)"

在 c# 中,您的匹配项将包含一组组,其中一个组将是 3 个字母

只要确保你"ABC"的字符串中没有任何意外的 s ,因为那会搞砸

这段代码

public static void Main()
{
    Regex regex = new Regex(".+ABC(...)");

    Match match = regex.Match("baln390nABCqlcln");
    foreach (Group group in match.Groups)
    {
        Console.WriteLine(group.Value);
    }
}

给出这个输出

baln390nABCqlc
qlc
Press any key to continue . . .
于 2012-11-21T23:03:29.673 回答
0
<tr class="text" .+ABC(?<capture>.{3})

结合RegexOptions.Singleline(以便.匹配换行符)。

于 2012-11-21T23:04:13.073 回答