如果术语在搜索文本中被换行符打断,我如何使用 RegEx 查找术语
How can I use RegEx to find a term, if the term is broken by a new-line in the searched text
假设我正在搜索 "applicants",正如我之前遇到的情况一样,我收到了这样一个文本文件:
We have considered the applica
nt's experience and qualification,
and wish to grant him an interview.
现在我仍然希望我的 RegEx return 匹配整个单词 "applicant" 的索引 23,并且我想告诉用户部分匹配从行 开始m 和列 n。我怎样才能做到这一点?
我想到的一个相当乏味的解决方案是在每次匹配之前插入一个特殊的标记字符,每次递增剩余匹配的索引。然后逐行重复搜索并查找标记后跟搜索词的第一个字符。
将换行符替换为“”。
快速而肮脏的方式:
applica\n?nt
如果您不知道换行符出现在哪里,请在每个字符之间添加它。
在搜索词的每个字符之间插入 [\t\r\n]*
(匹配定义集中的零个或多个字符)。然后,使用正则表达式匹配换行符 (@"\r?\n|\r"
) 将文本部分从 0 索引开始拆分到 match.Index
,这样就可以了:
var text = "Morelines\n\nWe have considered the applica\t\r\nnt's experience and qualification, \nand wish to grant him an interview.";
Console.WriteLine(string.Format("Our text:\n{0}\n---------", text));
var search = "applicant";
var pattern = string.Join(@"[\t\r\n]*", search.ToCharArray());
Console.WriteLine(string.Format("Our pattern: {0}\n----------", pattern));
var result = Regex.Match(text, pattern);
if (result.Success) {
Console.WriteLine(string.Format("Match: {0} at {1}\n----------", result.Value, result.Index));
var lineNo = Regex.Split(text.Substring(0, result.Index), @"\r?\n|\r").GetLength(0);
Console.WriteLine(string.Format("Line No: {0}", lineNo));
}
输出:
Our text:
Morelines
We have considered the applica
nt's experience and qualification,
and wish to grant him an interview.
---------
Our pattern: a[\t\r\n]*p[\t\r\n]*p[\t\r\n]*l[\t\r\n]*i[\t\r\n]*c[\t\r\n]*a[\t\r\n]*n[\t\r\n]*t
----------
Match: applica
nt at 34
----------
Line No: 3
假设我正在搜索 "applicants",正如我之前遇到的情况一样,我收到了这样一个文本文件:
We have considered the applica
nt's experience and qualification,
and wish to grant him an interview.
现在我仍然希望我的 RegEx return 匹配整个单词 "applicant" 的索引 23,并且我想告诉用户部分匹配从行 开始m 和列 n。我怎样才能做到这一点?
我想到的一个相当乏味的解决方案是在每次匹配之前插入一个特殊的标记字符,每次递增剩余匹配的索引。然后逐行重复搜索并查找标记后跟搜索词的第一个字符。
将换行符替换为“”。
快速而肮脏的方式:
applica\n?nt
如果您不知道换行符出现在哪里,请在每个字符之间添加它。
在搜索词的每个字符之间插入 [\t\r\n]*
(匹配定义集中的零个或多个字符)。然后,使用正则表达式匹配换行符 (@"\r?\n|\r"
) 将文本部分从 0 索引开始拆分到 match.Index
,这样就可以了:
var text = "Morelines\n\nWe have considered the applica\t\r\nnt's experience and qualification, \nand wish to grant him an interview.";
Console.WriteLine(string.Format("Our text:\n{0}\n---------", text));
var search = "applicant";
var pattern = string.Join(@"[\t\r\n]*", search.ToCharArray());
Console.WriteLine(string.Format("Our pattern: {0}\n----------", pattern));
var result = Regex.Match(text, pattern);
if (result.Success) {
Console.WriteLine(string.Format("Match: {0} at {1}\n----------", result.Value, result.Index));
var lineNo = Regex.Split(text.Substring(0, result.Index), @"\r?\n|\r").GetLength(0);
Console.WriteLine(string.Format("Line No: {0}", lineNo));
}
输出:
Our text:
Morelines
We have considered the applica
nt's experience and qualification,
and wish to grant him an interview.
---------
Our pattern: a[\t\r\n]*p[\t\r\n]*p[\t\r\n]*l[\t\r\n]*i[\t\r\n]*c[\t\r\n]*a[\t\r\n]*n[\t\r\n]*t
----------
Match: applica
nt at 34
----------
Line No: 3