语言= 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]之间的数据,也可以这样做。
新的RegEx,它将提取之间的数据
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之间提取数据。
最佳答案
(?<=\[STX\])(?:(?!\[STX\]).)*?(?=\[ETX\])
匹配
[STX]
和[ETX]
之间的任何文本(换行符除外):(?<=\[STX\]) # Are we right after [STX]? If so,...
(?: # match 0 or more of the following:
(?!\[STX\]) # (as long as it's not possible to match [STX] here)
. # exactly one character
)*? # repeat as needed until...
(?=\[ETX\]) # there is a [ETX] ahead.
在以下每个方面,这将始终与
somestring
匹配:blah blah [STX]somestring[ETX] blah blah
[STX]somestring[ETX] blah [STX]somestring[ETX] (hey, two matches here!)
[STX] not this! [STX]somestring[ETX] not this either! [ETX]
blah [ETX] [STX]somestring[ETX] [STX] bla bla
可以在Jan Goyvaerts出色的正则表达式教程(http://www.regular-expressions.info/lookaround.html)中找到有关正向/负向后看和超前断言(此正则表达式中使用了其中的三个)的完整参考。
关于c# - 如何使用C#在[STX]和[ETX]之间解析字符串-使用正则表达式或字符串函数分割/追加输出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3807155/