C#使用通配符

时间:2017-04-04 16:18:29

标签: c# string replace wildcard delimiter

解决: 该解决方案已经解决。我正在修改游戏并创建一个C#窗体,以便轻松操作大量重复数据或表达式,并快速将大量更改插入文件或文件。我坚持在我的数据文件中使用Regex识别正则表达式,并在我识别的表达式之后插入文本框的数据字段。感谢@Kris,我在正则表达式编辑器中的表达方式,但是当我将它部署到我的程序中时,没有任何反应。我为没有按顺序思考而道歉,下次使用SO时我肯定会更清楚。

下面是我想要操作的数据,接下来是工作代码我能用@Kris和@Rufus L的指针修复自己。再一次,我需要搜索特定的数据字符串并在其下面插入一个新的字符串文件中每次出现的数据属性。希望这有助于某人。

数据属性文本如下所示:

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
    }
}

我只想将文本插入到包含child = {}属性字段的父#### = {}属性中。 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 个答案:

答案 0 :(得分:1)

使用正则表达式

  

\ d +(。*?){

tested here

答案 1 :(得分:0)

另一种方法是在每个块的末尾插入文本,方法是搜索由}或更多空白字符分隔的两个0个字符。正则表达式如下:

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

在任何一种情况下,当插入字符串时,我们应该从 last 匹配开始(在字符串的末尾)并向后移动到开头,因为每个插入都将改变字符串长度,因此影响我们想要插入的索引。

例如:

/// <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 = "";
    }
}