[RESOLVED] Regex Syntax
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: [RESOLVED] Regex Syntax

Hybrid View

  1. #1
    Join Date
    Dec 2012
    Posts
    4

    [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:

    Subheader phrase

    Paragraph paragraph paragraph paragraph paragraph paragraph paragraph paragraph

    Subheader phrase

    Paragraph paragraph paragraph paragraph paragraph paragraph paragraph paragraph

    <span class="Title">Subheader phrase</span>
    I should be able to highlight "Subheader phrase" with the cursor, run the routine, and get this:

    <span class="Title">Subheader phrase</span>

    Paragraph paragraph paragraph paragraph paragraph paragraph paragraph paragraph

    <span class="Title">Subheader phrase</span>

    Paragraph paragraph paragraph paragraph paragraph paragraph paragraph paragraph

    <span class="Title">Subheader phrase</span>
    Here's the code so far:

    Code:
    string input = tbMain.SelectedText;
    string output = input;
    input = "<span class=\"Title\">" + input + "</span>";
                
    Regex rgx = new Regex(output + @"(?!</span>)");
    MatchCollection m = Regex.Matches(tbMain.Text, output + @"(?!</span>)");
    
    if (output.Length > 0 && m.Count > 0)
    {              
                tbMain.Text = rgx.Replace(tbMain.Text, "<span class=\"Title\">" + output + "</span>");
                lblStatus.Text = "Status: Successfully replaced " + m.Count + " subheadings";
    }
    else
    {
                tbMain.SelectedText = input;
    }
    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!

  2. #2
    Join Date
    Dec 2012
    Posts
    4

    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?

  3. #3
    Join Date
    Mar 2006
    Location
    Craiova, Romania
    Posts
    439

    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 please Rate his post and mark the thread as Resolved
    Please improve your messages appearance by using tags [ code] Place your code here [ /code]

  4. #4
    Join Date
    Dec 2012
    Posts
    4

    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?

  5. #5
    Join Date
    Dec 2012
    Posts
    4

    Re: Regex Syntax

    The Regex.Escape command worked perfectly, thanks for the help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center