C#使用通配符在每次出现文本字符串后插入文本
已解决: 这个解决方案已经解决了。我正在修改一个游戏,正在创建一个C#windows窗体,以便轻松处理大量重复数据或表达式,并快速将大量更改插入到一个或多个文件中。我一直在用Regex在数据文件中标识正则表达式,并在标识的表达式之后插入一个文本框数据字段。由于@Kris,我的表达式在正则表达式编辑器中工作,但是当我将它部署到我的程序中时,什么也没发生。我很抱歉没有整理好我的想法,下次我用它时一定会更清楚 下面是我想要处理的数据,接下来是我能够用@Kris和@Rufus L的指针修复的工作代码。同样,我需要搜索特定的数据字符串,并在文件中的每次出现的数据属性下插入一个新字符串。希望这对别人有帮助 数据属性文本如下所示:C#使用通配符在每次出现文本字符串后插入文本,c#,string,replace,wildcard,delimiter,C#,String,Replace,Wildcard,Delimiter,已解决: 这个解决方案已经解决了。我正在修改一个游戏,正在创建一个C#windows窗体,以便轻松处理大量重复数据或表达式,并快速将大量更改插入到一个或多个文件中。我一直在用Regex在数据文件中标识正则表达式,并在标识的表达式之后插入一个文本框数据字段。由于@Kris,我的表达式在正则表达式编辑器中工作,但是当我将它部署到我的程序中时,什么也没发生。我很抱歉没有整理好我的想法,下次我用它时一定会更清楚 下面是我想要处理的数据,接下来是我能够用@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={}属性字段的parent######={}属性中插入文本。IE,我想在1234={
感谢@Kris,我已经链接了正则表达式
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);
}
}
}
}
命名空间窗口窗体应用程序1{
公共部分类Form1:Form{
静态字符串[]文件;
静态int curNum=0;
静态字符串[]txtFiles;
静态字符串[]个省;
公共表格1(){
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e){
System.IO.File.writealText(pathText.Text+txtFiles[curNum],richTextBox1.Text);
}
private void prevButton_单击(对象发送者,事件参数e){
如果(curNum>0)
curNum--;
richTextBox1.Text=System.IO.File.ReadAllText(pathText.Text+txtFiles[curNum]);
Text=txtFiles[curNum];
}
私有无效加载按钮\u单击(对象发送者,事件参数e){
curNum=0;
txtFiles=GetFileNames(pathText.Text,“*.txt”);
richTextBox1.Text=System.IO.File.ReadAllText(pathText.Text+txtFiles[curNum]);
Text=txtFiles[curNum];
}
静态字符串[]GetFileName(字符串路径、字符串筛选器){
files=Directory.GetFiles(路径,过滤器);
for(int i=0;im.Value)
.ToArray();
对于(int i=0;i<1.Length;i++)
{
richTextBox1.Text=richTextBox1.Text.Replace(省[i],省[i]+System.Environment.NewLine+“”+textBox2.Text);
}
}
}
}
使用正则表达式
\d+(*?){
另一种方法是在每个块的末尾插入文本,搜索由
0
或更多空格字符分隔的两个}
字符。用于此操作的正则表达式如下所示:
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;
}
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 = "";
}
}
你的问题是什么?你的代码在哪里?对你的代码的模糊描述是没有帮助的。你遇到的具体问题是什么?为什么这听起来像是一个家庭作业问题,再一次!你知道关于对象或一般来说,关于集合的任何事情吗?描述是非常描述性的。如果你不理解,那么就不要t down vote,忽略它。append按钮将执行我的replace,但我不知道如何对数字和-=使用通配符{具有替换功能。您提供的代码只是表明您具有读取和写入文件的能力-似乎没有试图解决您自己的问题。因此,这不是一个代码编写服务。这很好,感谢regex tester链接!!!我能够对其进行排序。我要感谢您指出regex tester。这是真正的ly真的帮了我很多。我相信你在这个问题上节省了时间,因为最初我想帮助检测通配符,而你却帮了大忙。我考虑过像你建议的那样使用大括号,但属性可以涵盖多个细节级别,也就是说,可以有更多的通配符n任何给定文件或父属性中的一组尾随双括号。我给出的示例和游戏文件似乎没有使用这种做法,但在游戏文件的其他地方使用了省编号ID属性,我希望能够使用={}识别数字文本字符串的任何实例通过代码识别我正在使用的省属性或作用域。感谢您的回复,这确实有帮助。我最终使用Regex.Matches找到了我要查找的特定字符串。您的代码很好,但我确实需要能够嗅出本例中的数字。感谢您为我设置此建议,这真的很有帮助让我把字符串检测整理好。