Reputation: 13
I need to format some text entered by user.
For example: string str = "{{Lorem Ipsum|bold}}"
is simply dummy text of the printing and typesetting industry.
I have extracted Lorem Ipsum from the string using IndexOf
and displayed in Bold. But facing problem if I have two words to format in same string.
For example: string str ="{{Lorem Ipsum|bold}}"
is simply dummy text of the "{{printing|blue}}"
and typesetting industry.
Can someone help me out with any Regex pattern to extract {{Lorem Ipsum|bold}}
and {{printing|blue}}
as a array in c#.
Note: The pattern sometimes may be {{printing|blue,bold,http://www.google.com}}
.
Extension Method for formatting. I have hard coded for testing, later i'll optimize accordingly.
public static string Format(this string value)
{
System.Uri uriResult = null;
bool result = false;
string path = ConfigurationManager.AppSettings["path"];
if (value.Length > 0)
{
if (value.Contains("{{") && value.Contains("|") && value.Contains("}}"))
{
int totalLength = value.Length;
string unformattedText = value.Substring(value.IndexOf("{{"), (value.IndexOf("}}") - value.IndexOf("{{")) + 2);
string flowersRemoved = unformattedText.Substring(0, unformattedText.Length - 2);
flowersRemoved = flowersRemoved.Substring(2, flowersRemoved.Length - 2);
string[] textFormats = flowersRemoved.Split('|');
string text = textFormats[0];
string[] formats = textFormats[1].Split(',');
foreach (string format in formats)
{
result = Uri.TryCreate(format.ToLower(), UriKind.Absolute, out uriResult) && uriResult.Scheme == Uri.UriSchemeHttp;
if (format.ToLower() == "bold")
text = "<b>" + text + "</b>";
else if (format.ToLower() == "red")
text = "<font color = \"red\">" + text + "</font>";
else if (format.ToLower() == "blue")
text = "<font color = \"blue\">" + text + "</font>";
else if (result)
text = "<a href = \"" + format + "\" target = \"_blank\">" + text + "</a>";
else if (System.IO.File.Exists(path + format))
{
string fileName = System.IO.Path.GetFileName(path + format);
text = "<a href = \" \\s\\Download\\" + fileName + "\">" + text + "</a>";
}
}
value = value.Replace(unformattedText, text);
}
return value;
}
return value;
}
Upvotes: 1
Views: 375
Reputation: 141622
The pattern sometimes may be
{{printing|blue,bold,http://www.google.com}}
.
Try Split
.
The following fiddle uses Split()
not regex, because regex tends to be hard to read. The resultant key
string contains the word to format, and a values
string array contains the relevant settings.
string str = "{{printing|blue,bold,http://www.google.com}}";
// get your settings
int pFrom = str.IndexOf("{{") + "{{".Length;
int pTo = str.LastIndexOf("}}");
string settings = str.Substring(pFrom, pTo - pFrom);
// split
string[] parts = settings.Split('|');
string key = parts[0];
string[] values = parts[1].Split(',');
// for demo purposes only
Console.WriteLine(key);
foreach(var v in values)
{
Console.WriteLine("-" + v);
}
Output
printing
-blue
-bold
-http://www.google.com
If you tack this on the end you can have your desired HTML:
// create html
var html = string.Format(@"
<a href='{3}' target ='_blank'>
<font color='{1}'>
<b>{0}</b>
</font>
</a>", key, values[0], values[1], values[2]);
Console.WriteLine(html);
Output
<a href ='http://www.google.com' target = '_blank'>
<font color ='blue'>
<b>printing</b>
</font>
</a>
Upvotes: 1
Reputation: 13381
You can use next regexp with replace function
string str = "{{Lorem Ipsum|bold}}";
string path = ConfigurationManager.AppSettings["path"];
var formattedString = Regex.Replace(str, @"{{(?<symbol>[^|]+?)\|(?<formats>.+?)}}", m =>
{
var formatedPattern = m.Groups["formats"].Value.Split(',').Aggregate("{0}", (acc, f) =>
{
switch (f.ToLower())
{
case "bold": return "<b>" + acc + "</b>";
case "red": return "<font color = \"red\">" + acc + "</font>";
case "blue": return "<font color = \"blue\">" + acc + "</font>";
};
Uri uriResult;
if (Uri.TryCreate(f.ToLower(), UriKind.Absolute, out uriResult) && uriResult.Scheme == Uri.UriSchemeHttp)
{
return "<a href = \"" + f + "\" target = \"_blank\">" + acc + "</a>";
}
else if (System.IO.File.Exists(path + f))
{
string fileName = System.IO.Path.GetFileName(path + f);
return "<a href = \" \\s\\Download\\" + fileName + "\">" + acc + "</a>";
}
return acc;
});
return string.Format(formatedPattern, m.Groups["symbol"].Value);
});
Upvotes: 1