简体   繁体   English

如何使用正则表达式等在C#中处理文本框/富文本框中的文本

[英]How to process text from textbox/richtext box in C# using regular expression and more

Original Text in the txtinput will look in this format txtinput中的原始文本将采用这种格式

Firstportion Seconportion
Firstportion Seconportion
Firstportion Seconportion
Firstportion Seconportion
  1. My Goal is to first split each lines into two variable and add them to an array or list or whatever works [number of lines are variable so array will not work] 我的目标是首先将每行分成两个变量,然后将它们添加到数组或列表中或其他可行的方法[行数是可变的,因此数组将不起作用]

    var1[] = firstportion, var2=seconportion //so far looks like working for the 1st entry var1 [] = firstportion,var2 = seconportion //到目前为止,看起来像为第一个条目工作

  2. put those to variable array/list [Not working with the loops i currently have] 将它们放到变量数组/列表中[不使用我目前使用的循环]

  3. the process the first portions seperately and second portion seperatly with conditional if and regural expression and displaythem all back in the 2nd txtbox 使用条件if和正则表达式分别处理第一部分和第二部分,并将其全部显示在第二个txtbox中

     using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Collections; using System.Text.RegularExpressions; namespace Projectx { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string rawInput = txtInput.Text; //string[] rawInput = txtInput.Text.Split('\\n'); int x = 1; int y = 1; foreach (string line in txtInput.Lines) { string firstPortion= ""; string secondPortion = ""; string[] splitInput = Regex.Split(rawInput, ("\\\\s+")); firstPortion= splitInput[0]; secondPortion = splitInput[1]; //##### This works so far ans splits each line into two variables seperated by 1 white space but not sure if it works for 2nd line and seems can't assign these new splited values into two seperate array or list with the same loop or seperate loop //List<int> list = new List<int>; //(arr) List<string> myList1 = new List<string>(); List<string> myList2 = new List<string>(); myList1.Add(firtPortion); myList2.Add(secondPortion); txtOutPut.Text = "modified " + myList1[x] + " " + myList2[y]+"\\r\\n"; int i = 1; i++; x++; y++; } } } } 

Try declaring your : 尝试声明您的:

            List<string> myList1 = new List<string>();
            List<string> myList2 = new List<string>();

outside the foreach loop. 在foreach循环之外。

There seems to be two big problems. 似乎有两个大问题。

  1. You're creating a new pair of lists in each iteration rather than adding new items to the same list. 您将在每次迭代中创建一对新的列表,而不是将新项目添加到同一列表中。
  2. You're splitting the rawInput variable in each iteration rather than the current line . 您将在每次迭代中拆分rawInput变量,而不是在当前line拆分。

There are also a number of smaller ones mostly having to do with extraneous or unused variables. 还有一些较小的变量,主要与无关或未使用的变量有关。

Try something like this: 尝试这样的事情:

List<string> myList1 = new List<string>();
List<string> myList2 = new List<string>();
int x = 0;
foreach (string line in txtInput.Lines)
{
    string[] splitInput = Regex.Split(line, ("\\s+"));
    myList1.Add(splitInput[0]);
    myList2.Add(splitInput[1]);
    txtOutPut.Text += "modified " + myList1[x] + "  " + myList2[x] + Environment.NewLine;
    x++;
}
  1. The List should be declared outside the loop 该列表应在循环外声明
  2. Why do you use rawInput ? 为什么使用rawInput Instead you have to use line to do the Split 相反,您必须使用line进行分割
  3. You dont need the local variables(x,y) instead you could use LastOrDefault() 您不需要局部变量(x,y),而是可以使用LastOrDefault()

      List<string> myList1 = new List<string>(); List<string> myList2 = new List<string>(); string rawInput = txtInput.Text; foreach (string line in txtInput.Lines) { string firstPortion = ""; string secondPortion = ""; string[] splitInput = Regex.Split(line, ("\\\\s+")); firstPortion = splitInput[0]; secondPortion = splitInput[1]; myList1.Add(firstPortion); myList2.Add(secondPortion); txtOutPut.Text = "modified " + myList1.LastOrDefault() + " " + myList2.LastOrDefault() + "\\r\\n"; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM