Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 使用正则表达式提取包含搜索字符串的行_Regex - Fatal编程技术网

Regex 使用正则表达式提取包含搜索字符串的行

Regex 使用正则表达式提取包含搜索字符串的行,regex,Regex,串= "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

串=

"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.*)'
这会返回整个字符串,我只需要那一行。不知道我对正则表达式做了什么错事

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());
}

您可以使用CR和LFs分割文本,仅使用LINQ和
字符串获取包含所需文本值的文本。Contains

var results = inputText.Split(new[] {"\r", "\n"}, StringSplitOptions.RemoveEmptyEntries).Where(x => x.Contains("other than Inc Loans"));
要获得第一个“匹配项”,请使用
.FirstOrDefault()

详细信息

  • .Split(新[]{“\r”,“\n”},StringSplitOptions.RemoveEmptyEntries)
    -拆分为行
  • 。其中(x=>x.Contains(“公司贷款除外”)
    -提取包含特定文本的项目

如果您计划保留正则表达式,请确保您既不匹配LF字符也不匹配CR字符。模式应类似于
[^\r\n]*而不是Inc Loans[^\r\n]*
(其中
[^\r\n]
匹配除CR和LF字符以外的任何字符),并且在获得匹配后,通过
m.value

访问匹配值。是否确定原始字符串中存在换行符?它们是CRLF还是只是CRs?使用当前代码尝试
([^\r\n]*而不是Inc Loans[^\r\n]*)
(虽然此处不需要捕获组,但您可以使用
m.Value
)访问匹配项。另外,为什么要使用regex?请尝试
var result=inputText.Split(新[]{“\r”,“\n”},StringSplitOptions.RemoveEmptyEntries)。其中(x=>x.Contains(“除Inc贷款”)。FirstOrDefault()
。看见删除
.FirstOrDefault()
以获取包含所需文本的所有行。@WiktorStribiżew-您可以将此添加为答案,我将接受它吗
var result = inputText.Split(new[] {"\r", "\n"}, StringSplitOptions.RemoveEmptyEntries).Where(x => x.Contains("other than Inc Loans")).FirstOrDefault();