.net - C# greedy regular expression including match text -


i have string:

"cl,up_remove_line,#global_session_id,arg_stage=false,arg_project_id=-1,#global_line_id,arg_activity_id=-1,arg_mode=1,arg_line_id=#global_line_id,arg_session_id=-1" 

i tried:

splitty = regex.split(linetext,@"[\,]+\s*(?>arg_){1}?"); 

and received:

{string[7]}     [0]: "cl,up_remove_line,#global_session_id"     [1]: "stage=false"     [2]: "project_id=-1,#global_line_id"     [3]: "activity_id=-1"     [4]: "mode=1"     [5]: "line_id=#global_line_id"     [6]: "session_id=-1" 

i splitting @ least 1 comma, followed arbitrary white space followed "arg_" delimiter, there way keep "arg_" portion intact, i.e. indicies [1-6]?

use positive lookahead (to check presence of, not consume, leaving in split chunks) instead of atomic group (that still consumed, , removed when using split):

,+\s*(?=arg_) 

see regex demo

note not need put comma character class, nor need escape comma.

enter image description here

also, {1}? = {1} , totally redundant (you may remove since {1} implied (i.e. abc = a{1}b{1}c{1})).


Comments