I have a big text of 3 pages (SQL). And there are ERROR, WARNING, INFORMATION sections in the text. I want these sections in C#.Net by reading this text using REGEX. How can I do that. I donot know where each section ends.
Cant you go for 2 more extra columns for Warning and Information...that would make your life easier...parsing string with regex is not a cake walk, lot of testing needs to be done...if you have separate columns for every field, then you can fetch whatever
you want..
Does you get the text in exactly the same format as you have mentioned?
I mean to ask is that, every section comes in a new line or just next to previous section with space? Does the [D], [W] & [E] are surrounded by a single space or multiple spaces?
Sumit Pathak ------------------
ThisPost = Helped == True ? "Mark As Answer" : "Elaborate your problem in more details"
mchinta
Participant
1765 Points
523 Posts
Regex to find string in a text column
Feb 28, 2012 11:28 AM|LINK
I have a big text of 3 pages (SQL). And there are ERROR, WARNING, INFORMATION sections in the text. I want these sections in C#.Net by reading this text using REGEX. How can I do that. I donot know where each section ends.
Sum8
Contributor
4141 Points
931 Posts
Re: Regex to find string in a text column
Feb 28, 2012 11:41 AM|LINK
You text must be in a specific format, so that the Errors, Warnings and Information sections can be seperated out from this text.
Post the sample text with all the three sections.
Sumit Pathak
------------------
ThisPost = Helped == True ? "Mark As Answer" : "Elaborate your problem in more details"
Sajith G.S.
Member
236 Points
69 Posts
Re: Regex to find string in a text column
Feb 28, 2012 11:59 AM|LINK
gothough following link
http://msdn.microsoft.com/en-us/library/microsoft.win32.registry.getvalue.aspx
Thanks
Sajith GS
ramiramilu
All-Star
95403 Points
14096 Posts
Re: Regex to find string in a text column
Feb 28, 2012 03:14 PM|LINK
Cant you go for 2 more extra columns for Warning and Information...that would make your life easier...parsing string with regex is not a cake walk, lot of testing needs to be done...if you have separate columns for every field, then you can fetch whatever you want..
Thanks,
JumpStart
srinanthuram
Contributor
6800 Points
1549 Posts
Re: Regex to find string in a text column
Feb 28, 2012 05:19 PM|LINK
hi
http://jedit.org/users-guide/search-replace.html
thank u
mchinta
Participant
1765 Points
523 Posts
Re: Regex to find string in a text column
Feb 29, 2012 12:16 AM|LINK
17:09:04,17 [D] START SHOP
17:09:04,48 [W] Dictionary contains 26252 mappings
17:09:04,48 [D] Dictionary build time is: 0.25 seconds
17:09:04,5 [D] Mapping time took: 0.016 seconds
17:09:04,61 [E] Value cannot be null.
17:09:04,75 [E] The filename, directory name, or volume label syntax is incorrect.
17:09:04,75 [D] STOP SHOP
I want to get All [E] values, [W] values. And also dates of START SHOP and STOP SHOP
Sum8
Contributor
4141 Points
931 Posts
Re: Regex to find string in a text column
Feb 29, 2012 04:26 AM|LINK
Does you get the text in exactly the same format as you have mentioned?
I mean to ask is that, every section comes in a new line or just next to previous section with space? Does the [D], [W] & [E] are surrounded by a single space or multiple spaces?
Sumit Pathak
------------------
ThisPost = Helped == True ? "Mark As Answer" : "Elaborate your problem in more details"
Sum8
Contributor
4141 Points
931 Posts
Re: Regex to find string in a text column
Feb 29, 2012 08:40 AM|LINK
By considering that you have each section in new line and [D], [W] & [E] surrounded by single space, I have developed a solutions.
Try this:
StringBuilder originalText = new StringBuilder(); originalText.AppendLine("17:09:04,17 [D] START SHOP"); originalText.AppendLine("17:09:04,48 [W] Dictionary contains 26252 mappings"); originalText.AppendLine("17:09:04,48 [D] Dictionary build time is: 0.25 seconds"); originalText.AppendLine("17:09:04,5 [D] Mapping time took: 0.016 seconds"); originalText.AppendLine("17:09:04,61 [E] Value cannot be null."); originalText.AppendLine("17:09:04,75 [E] The filename, directory name, or volume label syntax is incorrect."); originalText.AppendLine("17:09:04,75 [D] STOP SHOP"); Regex r = new Regex(".*\\[D\\].*", RegexOptions.IgnoreCase | RegexOptions.Multiline); MatchCollection mcInformation = r.Matches(originalText.ToString()); List<string> listInformation = new List<string>(); foreach (Match objM in mcInformation) { listInformation.Add(objM.ToString().Replace("\r","")); } r = new Regex(".*\\[W\\].*", RegexOptions.IgnoreCase | RegexOptions.Multiline); MatchCollection mcWarnings = r.Matches(originalText.ToString()); List<string> listWarnings = new List<string>(); foreach (Match objM in mcWarnings) { listWarnings.Add(objM.ToString().Replace("\r", "")); } r = new Regex(".*\\[E\\].*", RegexOptions.IgnoreCase | RegexOptions.Multiline); MatchCollection mcErrors = r.Matches(originalText.ToString()); List<string> listErrors = new List<string>(); foreach (Match objM in mcErrors) { listErrors.Add(objM.ToString().Replace("\r", "")); }Sumit Pathak
------------------
ThisPost = Helped == True ? "Mark As Answer" : "Elaborate your problem in more details"
mchinta
Participant
1765 Points
523 Posts
Re: Regex to find string in a text column
Feb 29, 2012 12:14 PM|LINK
Also, how to grab START SHOP and STOP SHOP DATES???
Sum8
Contributor
4141 Points
931 Posts
Re: Regex to find string in a text column
Feb 29, 2012 12:47 PM|LINK
Try this:
Regex r = new Regex(".*\\[D\\].*", RegexOptions.IgnoreCase | RegexOptions.Multiline); MatchCollection mcInformation = r.Matches(originalText.ToString()); List<string> listInformation = new List<string>(); foreach (Match objM in mcInformation) { listInformation.Add(objM.ToString().Replace("\r","")); if (objM.ToString().Contains("START SHOP")) { Regex rStartDates = new Regex("((?:(?:[0-2]?\\d{1})|(?:[3][01]{1}))[-:\\/.](?:[0]?[1-9]|[1][012])[-:\\/.](?:(?:\\d{1}\\d{1})))(?![\\d])", RegexOptions.IgnoreCase | RegexOptions.Multiline); Match mStartDates = rStartDates.Match(originalText.ToString()); if (mStartDates.Success) { string strStartDate = mStartDates.Groups[0].ToString(); } } if (objM.ToString().Contains("STOP SHOP")) { Regex rStopDates = new Regex("((?:(?:[0-2]?\\d{1})|(?:[3][01]{1}))[-:\\/.](?:[0]?[1-9]|[1][012])[-:\\/.](?:(?:\\d{1}\\d{1})))(?![\\d])", RegexOptions.IgnoreCase | RegexOptions.Multiline); Match mStopDates = rStopDates.Match(originalText.ToString()); if (mStopDates.Success) { string strStopDate = mStopDates.Groups[1].ToString(); } } }Sumit Pathak
------------------
ThisPost = Helped == True ? "Mark As Answer" : "Elaborate your problem in more details"