字符串=
"A Borrower (or the Parent) may not deliver a Request if as a result of the proposed Utility:
[15] or more Term Loans [(other than Inc Loans)] would be outstanding; [or]
[10] or more Utilisations would be outstanding[; or
[5 ] or more Loans would be outstanding]."
Output = "[15] or more Term Loans [(other than Inc Loans)] would be outstanding;"
我正在尝试的正则表达式模式:
'(.*other than Inc Loans.*)'
这将返回整个STRING,我只需要一行。不知道正则表达式在做什么错
Asp.net中的代码
string regularExpressionPattern = RegExPattern.ToString();
string inputText = FinalPara.ToString();
Regex re = new Regex(regularExpressionPattern);
foreach (Match m in re.Matches(FinalPara))
{
Response.Write("Regex Values is :" + m.Groups[1].Value.ToString());
}
答案 0 :(得分:1)
您可以使用CR和LF分割文本,而仅使用LINQ和String.Contains
仅捕获包含所需文本值的文本:
var results = inputText.Split(new[] {"\r", "\n"}, StringSplitOptions.RemoveEmptyEntries).Where(x => x.Contains("other than Inc Loans"));
要获得第一个“匹配”,请使用.FirstOrDefault()
:
var result = inputText.Split(new[] {"\r", "\n"}, StringSplitOptions.RemoveEmptyEntries).Where(x => x.Contains("other than Inc Loans")).FirstOrDefault();
请参见C# demo online。
详细信息
.Split(new[] {"\r", "\n"}, StringSplitOptions.RemoveEmptyEntries)
-分成几行.Where(x => x.Contains("other than Inc Loans"))
-提取包含特定文本的项目。如果您打算保留正则表达式,请确保您不匹配LF和CR字符。模式应看起来像[^\r\n]*other than Inc Loans[^\r\n]*
(其中[^\r\n]
匹配除CR和LF字符以外的任何字符),并在获得匹配项后,通过m.Value
访问匹配值。