アプリ版:「スタンプのみでお礼する」機能のリリースについて

現在、C#を用いて不要な文字列を削除するプログラムを作成しています。
ネットなどで検索していろいろと実装してみましたが難しいです。どなたかご意見を頂けたら嬉しいです。
その文字列の文は以下のようなものです↓↓

pass1_best:こん pass1_best:こんに pass1_best: pass1_best:こんにちは

sentence1:こんにちは
pass1_best:おは pass1_best:おはよ pass1_best:おはよう

sentence1:おはよう
pass1_best:こん pass1_best:こんばんは

sentence1:こんばんは

このような文字列が並んでいます。私が抜き取りたいのは「sentence1:」以降の一文です。
ですので「pass1_best:」以下の文を削除したいと考えています。

また具体的な特徴として、
①一番最初は上の通り「pass1_best:」から始まっています。
②「pass1_best:」の後に1行の空白があり「sentence1:」が続いていきます。
③「sentence1:」のすぐ下の行に続いて「pass1_best:」が続いていきます。

これらのことを考えつつ「sentence1:」以下の分だけを残す(pass1_best:の文を削除、またはsentence1:以下の文を抽出)ことをして、

こんにちは
おはよう
こんばんは

のようにしていきたいと考えています。

長文になりましたが、ご意見をよろしくお願いします。

A 回答 (3件)

http://dobon.net/vb/dotnet/file/readfile.html
StreamReaderクラスで1行ずつ読み込んで、

http://dobon.net/vb/dotnet/string/regexmatch.html
正規表現で抽出して、表示(ファイルに書き出し?)する。
かと。
    • good
    • 0

別解です。



1. foreachで1行ずつ読み取り
 System.IO.File.ReadLines メソッド
 https://msdn.microsoft.com/ja-jp/library/dd38335 …

2. 読み取った行を、ifで特定の文字列(sentence1:)で始まるか判別し
 String.StartsWith メソッド
 https://msdn.microsoft.com/ja-jp/library/baketfx …

3. 指定位置(11文字、sentence1:の次)以降の文字列を取り出す
 String.Substring メソッド
 https://msdn.microsoft.com/ja-jp/library/hxthx5h …

4. 取り出した文字列を表示(ファイル書き出し?)する
    • good
    • 0

using System;


using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var value = @"pass1_best:こん pass1_best:こんに pass1_best: pass1_best:こんにちは

sentence1:こんにちは
pass1_best:おは pass1_best:おはよ pass1_best:おはよう

sentence1:おはよう
pass1_best:こん pass1_best:こんばんは

sentence1:こんばんは";

MatchCollection matches = Regex.Matches(value, @"(?<=sentence1:).+");

foreach(Match match in matches)
{
Console.WriteLine(match.Value);
}
Console.ReadLine();
}
}
}

【実行結果】
こんにちは
おはよう
こんばんは
    • good
    • 2

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!