regex - Regular Expression to split by VbCrLf + ignores VbCrLf within double quotes. VB.NET -


still trying understand regex. need split string on vbcrlf, not when inside double quotes.

so string built using stringbuilder below:

"abcdef"
"this is
sampletext"

so io stream , parse it. in io stream, single string parse "abcdef" vbcrlf "this vbcrlf sampletext"

now convert iostream string , want split this. required output is

"abcdef"
"this sampletext"

(if possible explain me expression, can understand , modify per needs)

thank you

description

i think easier use regex matching routine output each line. regex will:

  • assumes \r\n equivalent vbcrlf
  • matches entire lines , text
  • trims delimiting \r\n mimic how split command drop delimiter
  • keeps quoted strings if quoted string contains \r\n

^(?:[^"\r\n]|"[^"]*")*(?=\r\n|\z)

enter image description here

example

live demo of regex

sample text

line 1 line 2 line 3 "this  line 3a line 3b line 3c" , more text line 4 line 5 

code

vb.net code example: imports system.text.regularexpressions module module1   sub main()     dim sourcestring string = "replace source string"     dim re regex = new regex("^(?:[^""\r\n]|""[^""]*"")*(?=\r\n|\z)",regexoptions.ignorecase or regexoptions.ignorepatternwhitespace or regexoptions.multiline or regexoptions.singleline)     dim mc matchcollection = re.matches(sourcestring)     dim midx integer = 0     each m match in mc       groupidx integer = 0 m.groups.count - 1         console.writeline("[{0}][{1}] = {2}", midx, re.getgroupnames(groupidx), m.groups(groupidx).value)       next       midx=midx+1     next   end sub end module 

matches found

[0][0] = line 1 [1][0] = line 2 [2][0] = line 3 "this  line 3a line 3b line 3c" , more text [3][0] = line 4 [4][0] = line 5 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -