c# - Regex: ignore whitespace between fields -
i faced interesting c# string splitting problem. have below data require split key / value pairs. problem data not delimit on space characters well.
sample data:
somefield1:500 somefield2:atextfield somefield3:a text field spaces somefield4:102 somefield5whichisblank: somefeild6:m0redata somefeild7:(1,2,3 5) the approach attempting use matches delimiting space character using regex split:
var lineoftext = @"somefield1:500 somefield2:atextfield somefield3:a text field spaces somefield4:102 somefield5whichisblank: somefeild6:m0redata somefeild7:(1,2,3 5)" foreach (string match in regex.split(lineoftext, @"\s(?=[^\)]*(?:\(|$))").where(s => s != string.empty)) { // split key / value pairs here } the issue regex. think solution close, matching spaces in-between fields. gskinner example here.
if assist assist in fixing regex in order not match 'inbetween' spaces, or offer alternative method super.
thanks again.
try use regex: \s(?=\w+:)
Comments
Post a Comment