2017-04-04 8 views
0

解決: この解決方法は解決されました。私はゲームを改造し、大量の繰り返しデータや式を簡単に操作したり、大量の変更をファイルやファイルにすばやく挿入するためにC#Windowsフォームを作成していました。私はデータファイル内の正規表現で正規表現を識別し、識別した式の直後にテキストボックスのデータフィールドを挿入することに固執しました。私が正規表現エディタで動作している表現は、@ Krisのおかげですが、私がプログラムに展開したときには何も起こりませんでした。私は自分の考えを順番に持たないことをお詫びします。私は次の時にはっきりしています。C#ワイルドカードを使用してテキスト文字列が出現するたびにテキストを挿入します

以下は私が操作したいデータです。作業コードが続きます@Krisと@Rufus Lのポインタで自分自身を修正できました。また、特定のデータ文字列を検索し、新しい文字列を挿入する必要がありましたファイル内のすべてのオカレンスのdataプロパティ。これが誰かを助けることを望みます。

データプロパティのテキストは次のようになります。

1234 = { 
    workers = { 
     culture = dudes 
     religion = awesome 
     size = 37800 
    } 
    professionals = { 
     culture = dudes 
     religion = awesome 
     size = 6000 
    } 
    leaders = { 
     culture = dudes 
     religion = awesome 
     size = 500 
    } 
} 
1235 = { 
    workers = { 
     culture = dudes 
     religion = awesome 
     size = 37800 
    } 
    professionals = { 
     culture = dudes 
     religion = awesome 
     size = 6000 
    } 
    leaders = { 
     culture = dudes 
     religion = awesome 
     size = 500 
    } 
} 

私は親のみ#### = {}子を保持しているプロパティ= {}プロパティフィールドにテキストを挿入します。 IEの1234の新しい行にtextBox2.Textを挿入したい= {

@Krisのおかげでhereの正規表現がリンクされています。

namespace WindowsFormsApplication1 { 
    public partial class Form1 : Form { 
     static string[] files; 
     static int curNum = 0; 
     static string[] txtFiles; 
     static string[] provinces; 
     public Form1() { 
       InitializeComponent(); 
     } 
     private void button1_Click(object sender, EventArgs e) { 
      System.IO.File.WriteAllText(pathText.Text + txtFiles[curNum], richTextBox1.Text); 
     } 
     private void prevButton_Click(object sender, EventArgs e) { 
      if(curNum > 0) 
      curNum--; 
      richTextBox1.Text = System.IO.File.ReadAllText(pathText.Text + txtFiles[curNum]); 
      filenameLabel.Text = txtFiles[curNum]; 
     } 
     private void loadButton_Click(object sender, EventArgs e) { 
      curNum = 0; 
      txtFiles = GetFileNames(pathText.Text, "*.txt"); 
      richTextBox1.Text = System.IO.File.ReadAllText(pathText.Text + txtFiles[curNum]); 
      filenameLabel.Text = txtFiles[curNum]; 
     } 
     static string[] GetFileNames(string path, string filter) { 
      files = Directory.GetFiles(path, filter); 
      for (int i = 0; i < files.Length; i++) 
       files[i] = Path.GetFileName(files[i]); 
      return files; 
     } 
     private void nextButton_Click(object sender, EventArgs e) { 
      if(curNum < txtFiles.Length) 
      curNum++; 
      richTextBox1.Text = System.IO.File.ReadAllText(pathText.Text + txtFiles[curNum]); 
      filenameLabel.Text = txtFiles[curNum]; 
     } 
     private void appendButton_Click(object sender, EventArgs e) 
    { 
     provinces = Regex.Matches(richTextBox1.Text, @"\d+(.*?){") 
      .Cast<Match>() 
      .Select(m => m.Value) 
      .ToArray(); 
     for (int i = 0; i < provinces.Length; i++) 
     { 
      richTextBox1.Text = richTextBox1.Text.Replace(provinces[i], provinces[i] + System.Environment.NewLine + " " + textBox2.Text); 
     } 
    } 
    } 
} 
+2

あなたの質問は何ですか?あなたのコードはどこですか?あなたのコードのあいまいな説明は役に立ちません。あなたが直面している*具体的な問題は何ですか? – tnw

+0

なぜこれは宿題の質問のように聞こえるのですか?オブジェクトや一般的に、コレクションについては何か知っていますか? – TGarrett

+0

説明は非常に説明的です。あなたが理解していない場合は、投票しないで、無視してください。 – stu

答えて

1

のD + \正規表現

使用(。*?){

tested here

+0

これは面白いです、正規表現のテスターのリンクのおかげで! – stu

+0

私はそれを分類することができました。 Regexテスターを指摘してくれてありがとう。これはあなたが知ることのできるものを超えて私を本当に助けました。当初私はワイルドカードの検出に助けが必要でしたし、あなたが来たよりも多くのことをあなたに与えてくれました。 – stu

0

それを行うための別の方法は、それぞれの末尾にテキストを挿入することであろう0以上の空白文字で区切られた2つの}文字を検索することによってブロックすることができます。このため正規表現は次のようになります。いずれの場合も

Regex.Matches(searchString, "}\\s}"); 

、我々は(文字列の末尾)最後試合から始まり、最初の方に後方移動する必要があり、文字列を挿入する際に、各挿入理由文字列の長さが変更されるため、挿入を行うインデックスに影響を与えます。例えば

/// <summary> 
/// Returns a string with newText inserted into parentText 
/// </summary> 
/// <param name="newText">The new text to insert</param> 
/// <param name="parentText">The parent text to insert into</param> 
/// <returns>The parent string with the newText inserted</returns> 
private string GetInsertedText(string newText, string parentText) 
{ 
    var newParentText = parentText; 
    var matches = Regex.Matches(newParentText, "}\\s}"); 

    // Replace from the last occurrence, since each insert will 
    // change the length of our string and alter the insert location 
    for(int index = matches.Count - 1; index >= 0; index--) 
    { 
     var match = matches[index]; 

     newParentText = newParentText.Substring(0, match.Index + 1) + 
      Environment.NewLine + newText + 
      newParentText.Substring(match.Index + 1); 
    } 

    return newParentText; 
} 

また、それが挿入されています後、それが適切なインデントを持つようにtextBox1.Textの各行の先頭に4つのスペースを追加する方法を作成することもできます。たとえば:

private string GetIndentedText(string originalText, string indentText = " ") 
{ 
    return $"{indentText}{originalText}".Replace("\n", $"\n{indentText}").TrimEnd(); 
} 

これの使い方は次のようになります。

private void button1_Click(object sender, EventArgs e) 
{ 
    // Check for any text requirements here (like it should contain 
    // an open brace, an equals sign, end with a close brace, etc.) 
    if (textBox1.Text.Length > 0) 
    { 
     // Insert the new (indented) text into our RichTextBox 
     richTextBox1.Text = GetInsertedText(GetIndentedText(textBox1.Text), 
      richTextBox1.Text); 

     // Clear the input textbox 
     textBox1.Text = ""; 
    } 
} 
+0

私はあなたが提案したような末尾の中括弧を使用することを考えましたが、プロパティは1つ以上の詳細レベルを範囲指定できます。IEでは、任意のファイルまたは親プロパティに複数の末尾の二重ブレースを含めることができます。私が与えた例とゲームファイルはその習慣を利用していないようですが、州番号IDプロパティはゲームのファイルのどこかで使用されています。数字のテキスト文字列のインスタンスを識別できるようにしたいと思います= {}を使用して、私が州のプロパティまたはスコープで作業していることをコード経由で識別します。返信をありがとう、それは助けてください。 – stu

+0

Regex.Matchesを使用して、特定の文字列を検索しました。あなたのコードは面白かったですが、本当にこの場合に数字を盗聴できる必要がありました。その提案を私のために設定していただきありがとうございます。それは本当に私の文字列検出を順番に手伝ってくれました。 – stu

関連する問題