I have a text such as
string text = "Hi [img-2] tst[img-3] test ttteessstt grawr[img-1-800]tet";
I want to find img tags in the text and get their id (second value) and possibly the third value if it exists.
string text = "Hi [img-2] tst[img-3] test ttteessstt grawr[img-1-800]tet";
var th = new TagHandler(null);
var res = th.FindImgTags(text);
foreach(var tag in res)
{
Debug.WriteLine(string.Join(", ", tag.Tag, tag.Id, tag.Width));
}
The above code outputs this:
[img-2], 2, 0
[img-3], 3, 0
[img-1-800], 1, 800
This is my "find tags code". Its crude but gets the job done... Any suggestions on how to make this less verbose / brute
public List<ImgReplaceItem> FindImgTags(string data)
{
var result = new List<ImgReplaceItem>();
var pieces = data.Split(new string[] { "[img-" }, StringSplitOptions.RemoveEmptyEntries);
foreach (var item in pieces)
{
if (item.Contains(']'))
{
var backend = item.Split(new char[] { ']' }, StringSplitOptions.RemoveEmptyEntries);
var dataInside = backend[0];
var r = new ImgReplaceItem();
r.Tag = "[img-" + dataInside + "]";
var pid = -1;
if (dataInside.Contains('-'))
{
var widthParts = dataInside.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
int.TryParse(widthParts[0], out pid);
if (widthParts.Count() > 1)
{
var width = -1;
int.TryParse(widthParts[1], out width);
r.Width = width;
}
}
else
{
int.TryParse(dataInside, out pid);
}
r.Id = pid;
result.Add(r);
}
}
return result;
}