【问题标题】:Extract part of string where searchkeyword is found using regex提取使用正则表达式找到搜索关键字的部分字符串
【发布时间】:2018-10-31 18:10:36
【问题描述】:

字符串看起来像

最大使用次数

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.]

预期输出:

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

我尝试的方法似乎不起作用

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

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

【问题讨论】:

  • 你的“行”是用&lt;br/&gt;分隔的吗?那是HTML吗?最简单的方法是将所有&lt;br/&gt; 替换为\n,并使用我已经与您分享的非正则表达式解决方案。
  • 正确...这就是我们从数据库中取回字符串的方式...
  • 那么,为什么是正则表达式? inputText.Split(new[] {"&lt;br/&gt;"}, StringSplitOptions.None).Where(x =&gt; x.Contains("other than Incremental Facility Loans")).
  • @WiktorStribiżew - 谢谢,但是使用 REGEX 是否可行?
  • 是的,但是为什么呢?你能解释一下你在哪里使用它吗?正则表达式的性能/可读性将比纯代码差。

标签: regex


【解决方案1】:

纯正则表达式方法可能不够,因为您可能想进一步用换行符替换&lt;br/&gt;,而且模式相当复杂:

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

见regex demo

匹配:

  • (?&lt;=^|&lt;br/&gt;) - 以字符串开头或 &lt;br/&gt; 子字符串开头的位置
  • (?:(?!&lt;br/&gt;).)* - 任何字符,0+ 次,不以 &lt;br/&gt; 子字符串开头
  • other than Incremental Company Loans - 搜索字符串
  • [\s\S]*? - 任何 0+ 个字符,尽可能少
  • (?=[.;]&lt;br/&gt;|$) - 紧跟在 . 或 ; 后面紧跟 &lt;br/&gt; 或字符串结尾。

当您使用 C# 编写代码时,您可以使用可读性强且易于调整的non-regex solution:

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].

MagicTakeWhile 方法是从TakeWhile, but get the element that stopped it also 借来的。它需要项目直到满足条件包括停止满足条件的最后一个项目。

【讨论】:

  • 如何将 MagicTakeWhile 添加到 asp.net 网页...因为这是一个类方法
  • 你应该把它放到一个公共的静态类中。
  • @user3657339 很高兴它对你有用。如果我的回答对您有帮助,也请考虑投票。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-12
  • 1970-01-01
  • 2021-12-30
  • 1970-01-01
  • 2014-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多