c# - 如何使用 C# 解析 [STX] 和 [ETX] 之间的字符串 - 使用正则表达式或字符串函数拆分/附加输出

标签 c# regex parsing string

语言 = C#.NET

介于 [STX] 和 [ETX] 之间的任何东西都必须被接受,其余的东西必须被拒绝。

string startparam = "[STX]";
string endparam = "[ETX]";

String str1 = "[STX]some string 1[ETX]"; //Option 1
String str2 = "sajksajsk [STX]some string 2 [ETX] saksla"; //Option 2
String str3 = "[ETX] dksldkls [STX]some string 3 [ETX]ds ds"; //Option 3
String str4 = "dksldkls [STX]some string 4.1[ETX]ds ds [STX] some string 4.2[ETX] jdskjd"; //Option 4

/* the various strings can be appended and converted to a single 
   string using string builder or treat them as different strings*/

ProcessString (string str , string startparam , string endparam)
{
   //What To Write here using RegEX or String Functions in c#

}

/* The output after passing these to a ProcessString () */     
/* Append Output To a TextBox or Append it to a String using For Loop.*/

/* Output Required */

some string 1 
some string 2
some string 3
some string 4.1 
some string 4.2

============================================= ==============================

编辑 2

Language = C#

string str = "
[STX]some string 1[ETX]
sajksajsk [STX]some string 2 [ETX] saksla
[ETX] dksldkls [STX]some string 3 [ETX]ds ds
dksldk[STX]ls [STX]some st[ETX]ring 4.1[ETX]ds ds [STX]some string 4.2[ETX] jdskjd";

如果字符串数组是一个字符串,我如何获得相同的输出

/* output */
some string 1 
some string 2
some string 3
some string 4.1 
some string 4.2


/*case 1*/ 
the above string can be "[STX] djkdsj [STX]dskd1[ETX] dsnds[ETX]" 
the output should be just "dskd1"

/*case 2*/ 
the above string can be "[STX] djkdsj [STX]dskd1[ETX] ddd" 
the output should be just "dskd1"

/*case 3*/ 
the above string can be " kdsj [STX]dskd1[ETX] dsnds[ETX]" 
the output should be just "dskd1"

/*case 4*/ 
the above string can be "[STX] djk[STX]dsj [STX]dskd2[ETX] ddd" 
the output should be just "dskd2"

The real problem comes when [STX] followed by [STX] i want to consider the newer [STX] and start string processing from the newer [STX] occurance. Eg. Case 2 above

============================================= ==============================

编辑 3:新请求

语言 = C#

如果我想要[STX]和[STX]之间的数据也可以做到。

新的正则表达式将在两者之间提取数据 1.【STX】一些数据【STX】 2. [STX]一些数据[ETX]

例如。

/* the above string can be */
"[STX] djk[STX]dsj [STX]dskd2[ETX] ddd" 
/* the output should be just */
djk
dsj
dskd2

因为 [STX] 意味着传输已经开始,所以我也想在 STX 之间提取数据。

最佳答案

这对我有用:

string[] sepValues = input.Split(new char[] {'\u0002', '\u0003'},
                                 StringSplitOptions.RemoveEmptyEntries);

关于c# - 如何使用 C# 解析 [STX] 和 [ETX] 之间的字符串 - 使用正则表达式或字符串函数拆分/附加输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3807155/

相关文章:

c# - 为什么 C# 允许从 "new Shell32.Shell()"中的接口(interface)构造对象?

java - 正则表达式用引号替换字符串

c# - 使用正则表达式从 XML 字符串中删除 XML 节点命名空间前缀

java - 在 Java 中解析 JSON 字符串

c# - C#中的Excel单元格空格检查

c# - WCF 服务可以替代 TCPListener 的功能吗?

C# - 覆盖事件处理程序 - 添加参数

python - 正则表达式处理后在句末添加标点符号: Python

c++ - 用精神将日期时间字符串解析为 time_t 值

python - 使用python从文本文件中提取细节