If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

[RESOLVED] Regex Syntax

I'm writing a custom HTML mark-up form that will take in text and format it in a specified way. One of the functions is to add <span> tags to certain subheader phrases and words. What I want to do is to be able to highlight the subheader phrase and then use Regex to take the selected text and replace all other instances of it, except for those that already have span tags. For instance:

For the most part, it seems to be working, but I've found that it replaces partial matches as well. For instance, highlighting "Who" replaces any instance of "W" or "Wh" as well. Any thoughts as to how to fix this? I'm using .NET 4.0. Thanks in advance!

Re: Regex Syntax

Sorry for the double post, but the highlighted phrase I was having problems with is actually "Who?" and I realize now the question mark is probably screwing up the syntax. Do I need to escape the question mark? How would you do that with selected text?

Re: Regex Syntax

You can escape your string with Regex.Escape but I don't think that's your only problem. If you are just surrounding the current selection with <span> tag why don't you just replace your current selection with simple string operation using TextBox.SelectionStart and TextBox.SelectionLength ?

Bogdan

If someone helped you then pleaseRate his post and mark the thread as ResolvedPlease improve your messages appearance by using tags[ code] Place your code here [ /code]

Re: Regex Syntax

I wanted it to be able to take the selected string, search the rest of the document for the same string (the documents that it will be marking up will always have multiple instances of the subheader phrase) and then add span tags to those as well. Is there an efficient way to do that without regular expressions?