我有一个字符串:
1690年,本杰明·哈里斯(Benjamin Harris)在波士顿出版了《外国和外国的公共场合》。即使该报纸被政府压制之前,该报纸只发行了一个版本,但仍被认为是美国殖民地的第一份报纸。1704年,州长允许《波士顿新闻通讯》出版,并成为殖民地第一本连续出版的报纸。此后不久,每周的报纸开始在纽约和费城发表。这些早期的报纸遵循英国格式,通常为四页。他们主要传播来自英国的新闻,其内容取决于编辑的兴趣。1783年,《宾夕法尼亚晚报》成为第一本美国日报。
我想对我的程序进行编码,仅从上面的文本中提取一个句子。
例如,如果有人输入TextBox
单词“ governor”,则输出应显示:
1704年,州长允许《波士顿新闻通讯》出版,并成为殖民地第一本连续出版的报纸。
我已经尝试过自己做,到目前为止,我已经编写了代码:
string searchWithinThis = "In Boston in 1690, Benjamin Harris published Publick Occurrences Both Forreign and Domestick. This is considered the first newspaper in the American colonies even though only one edition was published before the paper was suppressed by the government. In 1704, the governor allowed The Boston News-Letter to be published and it became the first continuously published newspaper in the colonies. Soon after, weekly papers began publishing in New York and Philadelphia. These early newspapers followed the British format and were usually four pages long. They mostly carried news from Britain and content depended on the editor's interests. In 1783, the Pennsylvania Evening Post became the first American daily.";
string searchForThis = "governor";
int middle = searchWithinThis.IndexOf(searchForThis);
我的想法是,我可以找到第一个“。” 在“州长”一词之前,最后一个“。”。在单词'governor'之后,然后使用子字符串提取单词'governor'的句子。我不知道如何找到第一个和最后一个“ IndexOf”。在“州长”一词之间。
啊哈,正则表达式可以解救!
[^\.]*\bgovernor\b[^\.]*
片段:https://regex101.com/r/mB7fM7/2
代码:
static void Main(string[] args)
{
var textToSearch = "governor";
var textToSearchIn = "In Boston in 1690, Benjamin Harris published Publick Occurrences Both Forreign and Domestick. This is considered the first newspaper in the American colonies even though only one edition was published before the paper was suppressed by the government. In 1704, the governor allowed The Boston News-Letter to be published and it became the first continuously published newspaper in the colonies. Soon after, weekly papers began publishing in New York and Philadelphia. These early newspapers followed the British format and were usually four pages long. They mostly carried news from Britain and content depended on the editor's interests. In 1783, the Pennsylvania Evening Post became the first American daily.";
var pattern = String.Format("[^\\.]*\\b{0}\\b[^\\.]*", textToSearch);
if (Regex.IsMatch(textToSearchIn, pattern))
{
foreach (var matchedItem in Regex.Matches(textToSearchIn, pattern))
{
Console.WriteLine(matchedItem);
Console.WriteLine();
}
}
var lastMatch = Regex.Matches(textToSearchIn, pattern).Cast<Match>().Last();
Console.Read();
}
编辑:改进了使用\b
和Regex.MatchCollection
进行多个匹配的单词匹配的代码。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句