繁体   English   中英

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

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

txtinput中的原始文本将采用这种格式

Firstportion Seconportion
Firstportion Seconportion
Firstportion Seconportion
Firstportion Seconportion
  1. 我的目标是首先将每行分成两个变量,然后将它们添加到数组或列表中或其他可行的方法[行数是可变的,因此数组将不起作用]

    var1 [] = firstportion,var2 = seconportion //到目前为止,看起来像为第一个条目工作

  2. 将它们放到变量数组/列表中[不使用我目前使用的循环]

  3. 使用条件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++; } } } } 

尝试声明您的:

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

在foreach循环之外。

似乎有两个大问题。

  1. 您将在每次迭代中创建一对新的列表,而不是将新项目添加到同一列表中。
  2. 您将在每次迭代中拆分rawInput变量,而不是在当前line拆分。

还有一些较小的变量,主要与无关或未使用的变量有关。

尝试这样的事情:

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. 该列表应在循环外声明
  2. 为什么使用rawInput 相反,您必须使用line进行分割
  3. 您不需要局部变量(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