Last post Sep 03, 2009 10:35 AM by SGWellens
Sep 02, 2009 05:35 PM|akashenk|LINK
I am tasked with processing some text files that contain a whole bunch of snippet "inserts" of the form %%Some Info%%. Some example text is the following:
This book was written by %%Author_Name%% and was first published by Random House in %%Publication_Year%%.
I need to parse the text and find each of the %% blocks, so in this case, the results would be...
I have used regular expressions before which will allow me to find the content of curly braces, or other brackets, however in this case, the open and close bracket is the same "%%". Can any of you regular expression gurus give me hand in figuring out how
to extract these snippets? Thanks in advance!
Sep 02, 2009 09:42 PM|SGWellens|LINK
This is a bit klunky, but it works:
String Input = "This book was written by %%Author_Name%% and was first published by Random House in %%Publication_Year%%.";
MatchCollection Segments = Regex.Matches(Input, @"%%\w*%%");
foreach (Match Segment in Segments)
Response.Write(Segment.Value.Replace("%%", "") + "<br /> ");
Sep 03, 2009 10:17 AM|akashenk|LINK
That's not so clunky and works. Thanks for the reply. I just have one questoin. It doesn't come up in my scenario, but I was wondering what one would do if there was a possibility that the string surrounded by the %% contained some of the regular expression
reserved characters, like "*" or "?", etc. I gave it a try by escaping these characters using .replace (input, reservedchar, "\" + reservedchar) for each of the reserved characters I know of, but that didn't seem to work. The presence of a reserve character
in the segment between the %% seemed to prevent that segment from being found.
Sep 03, 2009 10:35 AM|SGWellens|LINK
\w means AlphaNumeric character. You can try adding other characters to the allowed set.
Use the tool whose link I sent you to design/test it (Expresso).