c# - How to search string for a specific substring and get a number associated with that substring? -
i newbie learning c# , task @ hand: have text file json in it. in text file there multiple lines , each paragraph separated next through empty line.
i want number associated substring "label " + number. see below.
this example of input text file:
{"menu": {"header": "menu", "items": [{"id": 27}, {"id": 0, "label": "label 0"}, null, {"id": 93}, {"id": 85}, {"id": 54}, null, {"id": 46, "label": "label 46"}]}}
{"menu": {"header": "menu", "items": [{"id": 81}]}}
{"menu": {"header": "menu", "items": [{"id": 70, "label": "label 70"}, {"id": 85, "label": "label 85"}, {"id": 93, "label": "label 93"}, {"id": 2}]}}
what i've done far is:
static void main(string[] args) { string path = @"c:\users\x\desktop\docomentul.txt"; string jsonstring; string resultstring; string labelstring = "label "; using (streamreader reader = file.opentext(path)) { jsonstring = reader.readtoend(); var jsonlines = regex.split(jsonstring,"(?m)^\\s*$"); foreach (var line in jsonlines) { resultstring = regex.match(line, @"\d+").value; console.writeline(resultstring); } } console.readkey(); }
now brings first occurrence of number in string. want find each occurrence of numbers associated substring "label ". @ least how planned on solving this. don't know how use regex nor how find in way.
once numbers associated substring "label ", have add them each item , print out sum each string in array (or paragraph).
the output test input should be:
46
0
248
Comments
Post a Comment