Click here to Skip to main content
14,733,955 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey. I was wondering if anyone knew where I could find some simple parsing code. I've done things like this before, and I know there are other things out there. Basically, I'm going to have a text box where admins can setup emails and i want them to be able to enter:
[FirstName]
[LastName]
[TextToUseIfWeOnlyHaveEmail]Sir Or Madam[/TextToUseIfWeOnlyHaveEmail]
I can certainly just do a text replace on FirstName and LastName, but the third one gets more confusing, so I tried to look for something online. I can't really think of what this would be called, so my i'm having trouble finding it... bleh. Monday mornings.
Posted

If you're using string.format could you use something like:
string email = "";
string str = string.Format("{0}\n{1}\n{2}...", "First name","Last name", string.IsNullOrEmpty(email) ? "": "Sir Or Madam");
   
Comments
fjdiewornncalwe 19-Dec-11 13:38pm
   
Based on the OP's question, this is absolutely correct. +5.
Wendelius 19-Dec-11 14:03pm
   
Thanks, although it seems that my 'guess' was incorrect :)
not really. I ended up just doing this. Still think that maybe there's some code out there to make this less manual.

RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Multiline;
var unnamed = new Regex(@"\[TextIfUnnamed\](.*?)\[\/TextIfUnnamed\]", options);
var loginLink = new Regex(@"\[LoginLink\](.*?)\[\/LoginLink\]", options);

var temp = emailTemplateBody;
temp = temp.Replace("[LastName]", user.LastName);
temp = temp.Replace("[FirstName]", user.FirstName);

temp = unnamed.Replace(temp, x => x.Groups[1].Value);
temp = loginLink.Replace(temp, x => MakeLink(loginUrl, x.Groups[1].Value));

private string MakeLink(string url, string text){ return String.Format("<a href='{0}'>{1}</a'>", url, text); }
   
Comments
fjdiewornncalwe 19-Dec-11 13:38pm
   
It would have been useful if you had actually told us you were trying to build a hyperlink from the data. By failing to mention that no one could have even guessed what you were actually trying to do.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900