所以,我有我的整个字符串(约10,000个字符),然后搜索该字符串的一个字(或多个字)。随着正则表达式(字).Matches(scrappedstring)
。
So I have got a whole string (about 10k chars) and then searching for a word(or many words) in that string. With regex(word).Matches(scrappedstring)
.
但如何做到这一点,提取整个句子,包含了这个词汇。我想采取一个子串的搜索词,直到第一个点/感叹号/问号/等之后。但如何在搜索词前采取句子的一部分?
But how to do so to extract the whole sentence, that contains that word. I was thinking of taking a substring after the searched word until the first dot/exclamation mark/question mark/etc. But how to take the part of the sentence before the searched word ?
或者,也许有更好的逻辑?
Or maybe there's a better logic ?
推荐答案
如果您的边界是如。
,!
,?
和;
,搭配跨 [^;。!?] *(wordmatch)[^;。!?] * EX pression。
它会给所有的句子所需的 wordmatch 的里面。
If your boundaries are e.g. .
, !
, ?
and ;
, match all sentences across [^.!?;]*(wordmatch)[^.!?;]*
expression.
It will give all sentences with desired wordmatch inside.
例如:
var s = "First sentence. Second with wordmatch ? Third one; The last wordmatch, EOM!";
var r = new Regex("[^.!?;]*(wordmatch)[^.!?;]*");
var m = r.Matches(s);
var result = Enumerable.Range(0, m.Count).Select(index => m[index].Value).ToList();
相关推荐
最新文章