2016-04-09 10 views
1

私は、文字列があります。1690年にボストンでC#の特定の単語を含む文字列からテキストを取得するには?

を、ベンジャミン・ハリスは、ブリック出現にForreignとDomestick両方を発表しました。これは、政府が紙を押さえる前に、ただ1つの版が出版されたにもかかわらず、アメリカ植民地の最初の新聞と考えられています。ガバナーは1704年にThe Boston News-Letterを公表し、植民地で最初に出版された新聞となった。すぐ後、週刊紙がニューヨークとフィラデルフィアで出版され始めました。これらの初期の新聞はイギリスの書式に従い、通常4ページの長さでした。彼らは主に英国からのニュースや編集者の関心に依存したコンテンツを運営していました。 1783年、ペンシルベニアのイブニングポストは、アメリカで最初のアメリカ人となった。

私は上記の文章から1つの文を抽出するために自分のプログラムをコーディングする必要があります。

例えば、TextBox単語 `知事の誰かの種類は、出力が示す必要がある場合: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の最初と最後の検索方法はわかりません。言葉「知事」の間IndexOfよう最適化されたとして、それはしかしではありませんので、あなたは非常に長いテキストを持っている場合、それは問題になる可能性が

var sequence = searchWithinThis.Split('.').FirstOrDefault(s => s.Contains(searchForThis)); 

答えて

2

なるほど、救助への正規表現:

そうしないと、あなたのような何かを行うことができます!

[^\.]*\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(); 
} 

EDIT:複数の一致する\bRegex.MatchCollectionを使用してワード整合のためのコードを改善しました。

+0

うわー、それは完璧に動作します!ありがとうございました。 私は「ガバナー」という言葉が3回使用された場合はどうなりますか、私はこの言葉で最後の文章を得たいと思います。 "1704年、知事はThe Boston News-Letterを公表し、植民地で初めて出版された新聞(総裁)になりました。通常は4ページの長さであった(ガバナー)。" そして、私だけを抽出したい: "これらの初期の新聞は、英国の書式に従い、通常は長さが4ページ(総裁)だった。 " – ktos1234

+0

更新された回答を見る。 – grmbl

+0

前にRegexライブラリについて聞いたことはありませんが、それは非常に便利です。 – ktos1234

1

一つの方法は、正しいものを見つけ、シーケンスに文字列を分割することであろう。

var index = searchWithinThis.IndexOf(searchForThis); 

if (index != -1) 
{ 
    int startIndex = 0; 
    int endIndex = searchWithinThis.Length; 

    for (int i = index + searchForThis.Length; i < searchWithinThis.Length; i++) 
    { 
     if (searchWithinThis[i] == '.') 
     { 
      endIndex = i; 
      break; 
     } 
    } 

    for (int i = index - 1; i >= 0; i--) 
    { 
     if (searchWithinThis[i] == '.') 
     { 
      startIndex = i + 1; 
      break; 
     } 
    } 

    var sequence = searchWithinThis.Substring(startIndex, endIndex - startIndex); 
} 
+0

このコードも同様ですが、私はregexライブラリを使用することにしました。ありがとう。 – ktos1234

関連する問題