Regex 提取使用正则表达式找到searchkeyword的字符串部分

Regex 提取使用正则表达式找到searchkeyword的字符串部分,regex,Regex,绳子看起来像 最大使用次数 A Borrower (or the Parent) may not deliver a Utilisation Request if as a result of the proposed Utilisation:<br/> [10] or more Term Loans [(other than Incremental Company Loans)] would be outstanding; [or]<br/> [15] or more

绳子看起来像

最大使用次数

A Borrower (or the Parent) may not deliver a Utilisation Request if as a result of the proposed Utilisation:<br/>
[10] or more Term Loans [(other than Incremental Company Loans)] would be outstanding; [or]<br/>
[15] or more Revolving Company Utilisations would be outstanding[; or<br/>
[20] or more Incremental Company Loans would be outstanding].<br/>
A Borrower (or the Parent) may not request that a Company A Loan [or an Incremental Company Loan] be divided if, as a result of the proposed division, [  25      ] or more Company A Loans [or [  50    ] or more Incremental Company Loans] would be outstanding.<br/>
[A Borrower (or the Parent) may not request that a Company B Loan or a Company C Loan be divided.]
我所尝试的似乎不起作用

Regex = '.*other than Incremental Company Loans.*'

这将返回整个段落。可能还有其他方法可以做到这一点,但我们只能使用正则表达式来做到这一点。

纯粹的正则表达式方法可能不够,因为您可能希望进一步用换行符替换

,而且模式相当复杂:

(?<=^|<br/>)(?:(?!<br/>).)*other than Incremental Company Loans[\s\S]*?(?=[.;]<br/>|$)
输出:

[10] or more Term Loans [(other than Incremental Company Loans)] would be outstanding; [or]
[15] or more Revolving Company Utilisations would be outstanding[; or
[20] or more Incremental Company Loans would be outstanding].

magictakwhile
方法是借用自。它接受项目直到满足条件,包括最后一个不再满足条件的项目。

您的“行”是否用

分隔?那是HTML吗?最简单的方法是将所有

替换为
\n
,并使用我已与您共享的非正则表达式解决方案。正确。。。这就是我们从数据库中获取字符串的方式……那么,为什么要使用正则表达式呢
inputText.Split(new[]{“
”,StringSplitOptions.None)。其中(x=>x.Contains(“除了增量贷款”)
@WiktorStribiżew-谢谢,但是使用REGEX可以吗?是的,但为什么?你能解释一下你在哪里用它吗?与纯代码相比,正则表达式的性能/可读性较差。如何将MagicTakeWhile添加到asp.net网页。。。bcos这是一个类方法,您应该将它放入一个公共静态类中。@user3657339很高兴它能为您工作。如果我的回答对你有帮助,请考虑一下投票。
var s = "A Borrower (or the Parent) may not deliver a Utilisation Request if as a result of the proposed Utilisation:<br/>[10] or more Term Loans [(other than Incremental Company Loans)] would be outstanding; [or]<br/>[15] or more Revolving Company Utilisations would be outstanding[; or<br/>[20] or more Incremental Company Loans would be outstanding].<br/>A Borrower (or the Parent) may not request that a Company A Loan [or an Incremental Company Loan] be divided if, as a result of the proposed division, [  25      ] or more Company A Loans [or [  50    ] or more Incremental Company Loans] would be outstanding.<br/>[A Borrower (or the Parent) may not request that a Company B Loan or a Company C Loan be divided.]";
var result = s.Split(new[] {"<br/>"}, StringSplitOptions.None)
    .SkipWhile(x => !x.Contains("other than Incremental Company Loans"))
    .MagicTakeWhile(x => !x.EndsWith(".") && !x.EndsWith(";"));
Console.WriteLine(string.Join("\n", result));
[10] or more Term Loans [(other than Incremental Company Loans)] would be outstanding; [or]
[15] or more Revolving Company Utilisations would be outstanding[; or
[20] or more Incremental Company Loans would be outstanding].