Saturday 15 September 2012

C# Parsing Keyword lines for output -



C# Parsing Keyword lines for output -

we attempting create alter log of microsoft kbase updates applying during out normal maintenance cycles. want parse info below particular lines. sample below:

operation : 1 resultcode : 2 hresult : 0 date : 10/7/2014 10:27:50 updateidentity : system.__comobject title : update microsoft silverlight (kb2977218) description : update silverlight improves security, reliability, accessibility support, startup performance, enhances line-of-business back upwards , includes several fixes improve back upwards rich net applications. update backward compatible web applications built using previous versions of silverlight. unmappedresultcode : 0 clientapplicationid : automaticupdates serverselection : 1 serviceid : uninstallationsteps : system.__comobject uninstallationnotes : supporturl : http://go.microsoft.com/fwlink/?linkid=105787 categories : system.__comobject

our desired output is:

title : update microsoft silverlight (kb2977218) date : 10/7/2014 10:27:50 description : update silverlight improves security, reliability, accessibility support, startup performance, enhances line-of-business back upwards , includes several fixes improve back upwards rich net applications. update backward compatible web applications built using previous versions of silverlight.

i trying write simple c# application paste raw info rich text box click button , have desired output in rich text box. there pattern of "keyword : data" might useful.

i have form created , elements on form. have attempted find method search keyword not yield result seek. want keyword or line speak , can see description multiple lines.

i don't have sample code post don't know begin given task. sample code helpful accomplish task.

you can seek next regex pattern:

(?<=\b[keyword]\b\s*:\s*).*

simply replace [keyword] actual keyword you're looking for. illustration (?<=\btitle\b\s*:\s*).* homecoming update microsoft silverlight (kb2977218). here's how you'd utilize in code:

private string getdatafromkeyword(string source, string keyword) { homecoming regex.match(source, string.format(@"(?<=\b{0}\b\s*:\s*).*", keyword)).value.trim(); }

and phone call like:

string info = getdatafromkeyword(textbox.text, "title");

explanation of pattern:

(?<=): notation positive look-behind.

\b[keyword]\b\s*:\s*: matches entire word [keyword] followed number of spaces followed : followed number of spaces.

.*: matches after behind, data in keyword: data pair.

edit

if have multiple instances of given keyword, can utilize matches() method instead of match():

private ienumerable<string> getdatafromkeyword(string source, string keyword) { homecoming regex.matches(source, string.format(@"(?<=\b{0}\b\s*:\s*).*", keyword)) .cast<match>().select(match => match.value.trim()); }

now var info = getdatafromkeyword(textbox.text, "title"); returns list of matches can enumerate through:

var titles = getdatafromkeyword(textbox.text, "title").toarray(); var dates = getdatafromkeyword(textbox.text, "date").toarray(); var descriptions = getdatafromkeyword(textbox.text, "description").toarray(); (int = 0; < titles.count(); i++) { string block = string.format("title: {0}, date: {1}, description: {2}", titles[i], dates[i], descriptions[i]); messagebox.show(string.format("block {0}: {1}", i+1, block)); }

note assume have same number of title, date, , description entries. i'm not sure requirements are, illustration of iterating on lists. alter based on needs.

c# parsing richtextbox

No comments:

Post a Comment