循环程序以捕获和替换值 [英] Loop Program to capture and replace the value

查看:52
本文介绍了循环程序以捕获和替换值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事一个自动化项目,该项目需要一个循环程序,该循环程序从一行中捕获值,然后在另一行中替换它.

我在一个文本框内有一个内容(最多可以包含2000页),其中包含一些SGML标记,如下所示:

I am working on a automation project which requires a loop program that captures the value from one line and replaces it in another line.

I have a content(can be upto 2000 pages) within a textbox, which has some SGML tagging like below:

lsec prefix='Article' d='3' uid='VAT1011_A03'

lsbsec d='1.' loc='VAT1011_00_01' Goods produced 
lsbsec d='2.' loc='VAT1011_00_02' Goods used 
lsbsec d='3.' loc='VAT1011_00_03' Business entity 
lsbsec d='4.' loc='VAT1011_00_04' Consigns goods

lsec prefix='Article' d='4' uid='VAT1011_A04'

lsbsec d='1.' loc='VAT1011_00_01' Goods produced 
lsbsec d='2.' loc='VAT1011_00_02' Goods used 
lsbsec d='3.' loc='VAT1011_00_03' Business entity 
lsbsec d='4.' loc='VAT1011_00_04' Consigns goods



通过按钮单击,需要捕获lsec行中突出显示的部分,并将其粘贴到以下lsbsec行中的突出显示的部分( 00 )中,直到下一个lsec开始.

需要捕获的唯一值是_和('')之间的值.例如: _A03''.

lsbsec行中需要替换的唯一值是 _00 _ .



Through a button click highlighted part from lsec line needs to be captured and pasted in the highlighted part (00) in the following lsbsec lines till the next lsec starts.

The unique value that needs to be captured is the value between _and (''). Eg : _A03''.

Unique value that needs to be replaced is _00_ in the lsbsec lines.

推荐答案

我已经完成了从lsec捕获​​值的操作行,当我尝试在lsbsec中替换_00_时,请编程替换所有值.

实际上,它只需要替换,直到下一个lsec开始.我写了一个条件,它不是循环程序."


因此,大概您正在使用String.Replace?

这不会太好-替换方法没有上限或下限,可以设置将替换操作限制为文本的选定部分.

有两种方法可以执行此操作.
如果仅尝试替换单个部分,那么我将找到开始的lsec行,结束的lsec行,然后将输入字符串切成三部分,然后再更改单个部分并重新构建它. >
如果您有多个工作要做,那么我建议使用以下两种方法之一:
1)使用String.Split将文本分为以lsec部分开头的部分,并使用String.Replace进行更新.然后使用String.Join重建输出.
2)使用Regex-Regex.Replace方法有一个版本,该方法使用MatchEvaluator方法来更新替换字符串的每个实例,您可以使用它们来匹配lseclsbsec行.我怀疑这将是正确的事情,但是这将是易于维护的版本.我将其用于替换字符串参数:高级Regex.Replace处理 [ ^ ]-链接无法解决您的问题问题,但它显示了我对匹配评估程序的处理方式以及设置过程的简便性.
"i have completed capturing the value from lsec line, and when i tried to replace _00_ in lsbsec, program replacing all the values.

Actually it has to replace only till next lsec starts. i wrote a condition And it is not a loop program."


So presumably you are using String.Replace?

That won''t work too well - the replace method has no upper or lower bounds you can set to limit the replace operation to a selected portion of the text.

There are a couple of ways you can do this.
If you are trying to replace a single section only, then I would locate the start lsec line, located the end lsec line, then cut the input string into three parts before changing the single section and rebuilding it.

If you have multiple sections to do, then I would suggest one of two routes:
1) Use String.Split to break the text into sections starting with a lsec section, and use String.Replace to update each. Then use String.Join to rebuild the output.
2) Use a Regex - there is a version of the Regex.Replace method which uses a MatchEvaluator method to update each instance of the replace string which you could use to match your lsec and lsbsec lines. It would be a faff to get right I suspect, but it would be an easy to maintain version. I use this for string parameter replacement: Advanced Regex.Replace handling[^] - the link won''t solve your problem, but it shows what I do with the match evaluator, and how easy it is to set up.


private void button1_Click(object sender, EventArgs e)
                 {

                          int i;
                          string value = string.Empty;
                          string finalValue = string.Empty;

                          for (i = 0; i < textBox1.Lines.Count(); i++)
                          {
                                   string lineText = textBox1.Lines[i];

                                   if(lineText.Contains("lsec"))>
                                   {
                                            value = string.Empty;
                                            int index = lineText.IndexOf(textBox2.Text, System.StringComparison.Ordinal);
                                            int correctindex = index + textBox2.Text.Length + 1;


                                            string text = lineText.Substring(correctindex, lineText.Length - correctindex);

                                            for (int j = 0; j < text.Length; j++)
                                           {
                                                    if (Char.IsLetterOrDigit(text[j]))
                                                    {
                                                             value += text[j];
                                                    }
                                            }

                                   }
                                   else
                                   {
                                            if (lineText.Contains("00"))
                                            {
                                                    lineText = lineText.Replace("00", value);
                                            }
                                   }

                                   finalValue += lineText + Environment.NewLine;
                          }

                          textBox1.Text = finalValue;
                 }


这篇关于循环程序以捕获和替换值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆