如何在一个字符串中的一个单词匹配提取完​​整的句子? [英] how to extract a whole sentence by a single word match in a string?

查看:123
本文介绍了如何在一个字符串中的一个单词匹配提取完​​整的句子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有我的整个字符串(约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();

这篇关于如何在一个字符串中的一个单词匹配提取完​​整的句子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆