How Parsing string between [STX] and [ETX] using C# - Split/Append output using Regex or String Functions -



How Parsing string between [STX] and [ETX] using C# - Split/Append output using Regex or String Functions -

language = c#.net

anything between [stx] , [etx] must accepted rest of things must rejected.

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] string 4.2[etx] jdskjd"; //option 4 /* various strings can appended , converted single string using string builder or treat them different strings*/ processstring (string str , string startparam , string endparam) { //what write here using regex or string functions in c# } /* output after passing these processstring () */ /* append output textbox or append string using loop.*/ /* output required */ string 1 string 2 string 3 string 4.1 string 4.2

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

edit 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";

how can same output if string array 1 single string

/* output */ string 1 string 2 string 3 string 4.1 string 4.2 /*case 1*/ above string can "[stx] djkdsj [stx]dskd1[etx] dsnds[etx]" output should "dskd1" /*case 2*/ above string can "[stx] djkdsj [stx]dskd1[etx] ddd" output should "dskd1" /*case 3*/ above string can " kdsj [stx]dskd1[etx] dsnds[etx]" output should "dskd1" /*case 4*/ above string can "[stx] djk[stx]dsj [stx]dskd2[etx] ddd" output should "dskd2" real problem comes when [stx] followed [stx] want consider newer [stx] , start string processing newer [stx] occurance. eg. case 2 above

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

edit 3 : new request

language = c#

if want info between [stx] , [stx] can done.

new regex extract info between 1. [stx] info [stx] 2. [stx] info [etx]

eg.

/* above string can */ "[stx] djk[stx]dsj [stx]dskd2[etx] ddd" /* output should */ djk dsj dskd2

as [stx] means transmission has been started want extract info between stx well.

(?<=\[stx\])(?:(?!\[stx\]).)*?(?=\[etx\])

matches text (except newlines) between [stx] , [etx]:

(?<=\[stx\]) # right after [stx]? if so,... (?: # match 0 or more of following: (?!\[stx\]) # (as long it's not possible match [stx] here) . # 1 character )*? # repeat needed until... (?=\[etx\]) # there [etx] ahead.

this match somestring in each of following:

blah blah [stx]somestring[etx] blah blah [stx]somestring[etx] blah [stx]somestring[etx] (hey, 2 matches here!) [stx] not this! [stx]somestring[etx] not either! [etx] blah [etx] [stx]somestring[etx] [stx] bla bla

a total reference on positive/negative lookbehind , lookahead assertions (three of used in regex) can found in jan goyvaerts' first-class regular look tutorial @ http://www.regular-expressions.info/lookaround.html.

c# regex parsing string

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -