simshp
asked on
Insert html into a string
Hello there ..
I am not sure how to go about this regex problem. I need to insert <strong> ... </strong> tags into a string at a specific point eg:
Every string has the following pattern: "You want to make a change! You think about y... " - a string of words until the "!" and then further text.
I need to insert the <strong> tag at the start of the string and then </strong> after the "!" character. Is there a way to do this in with one pass over the whole string, returning the desired result ?
Thanks
(using vb.net 2003)
I am not sure how to go about this regex problem. I need to insert <strong> ... </strong> tags into a string at a specific point eg:
Every string has the following pattern: "You want to make a change! You think about y... " - a string of words until the "!" and then further text.
I need to insert the <strong> tag at the start of the string and then </strong> after the "!" character. Is there a way to do this in with one pass over the whole string, returning the desired result ?
Thanks
(using vb.net 2003)
First problem is that you have to define what characters belong to the sentance ending with the exclamation mark. My first guess would be: all lovercase, uppercase characters, blanks, coma, dash, but excluding dot, xclamation char and quota characters. So you see it is diffecult to make it 100 percent matching.
Unfortunatley I'm not at an environment that I can test this with, but I think the following regex should get you close to what you want. You can use it to find all sentences that begin with an upper case character and end with an exclamation point. You should be able to use it in conjunction with the Regex.Replace.
.*[A-Z]+.*!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, I based my solution on this answer
Dim s as String = "You want to make a change! You think about y... "
s = s.Insert(IndexOf("!", s)+1, "</strong>").Insert(0, "<strong>")
but always you have to check if the ! is present, first:
If (s.Contains("!") Then s = s.Insert(IndexOf("!", s)+1, "</strong>").Insert(0, "<strong>")