Replacing Text without changing case??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • wildman@noclient.net

    Replacing Text without changing case??

    RE: Replacing Text without changing case??

    This code works great, but case has to be exact.

    Research.Text = Research.Text.R eplace(textboxS earch.Text, "<b>" +
    textboxSearch.T ext + "</b>")



    I found some c# code, but I can't make it work in vb.net:

    Regex.Replace(R esearch.Text, textboxSearch.T ext, New
    MatchEvaluator( HiLite), RegexOptions.Ig noreCase)


    ....
    Public Shared Function HiLite(ByVal match As Match) As String
    Return "<b>" + match.Value + "</b>*"
    End Function


    Its complaining about Hilite in the first line.


  • wildman@noclient.net

    #2
    Re: Replacing Text without changing case??

    THANK YOU!

    Regex.Replace(s tring.format("( {0})", Regex.Escape(in put)), "<b>$1</b>", RegexOptions.Ig noreCase)
    Presuming

    user search input : textboxSearch.t ext
    replace is : "<b>"+textboxSe arch.text+"</b>"
    data I'm looking to impact is : Research.Text (a gridview row label
    control)


    How would I use the above?

    I tried:

    Imports System.Text.Reg ularExpressions

    Research.Text = Regex.Replace(S tring.Format("( {0})",
    Regex.Escape(te xtboxSearch.Tex t)), "<b>$1</b>",
    RegexOptions.Ig noreCase)


    Getting an error on Regex.Replace : Reference to a non-shared member
    requires an object reference.

    So I noticed it appears to be formated incorrectly and probably should
    be :

    Research.Text = Regex.Replace(S tring.Format("( {0})"),
    Regex.Escape(te xtboxSearch.Tex t), "<b>$1</b>",
    RegexOptions.Ig noreCase)

    However that did not work.

    I also tried:


    No effect:
    Research.Text = Regex.Replace(S tring.Format("( {0})"),
    Regex.Escape(te xtboxSearch.Tex t), "<b>$1</b>",
    RegexOptions.Ig noreCase)


    replaces string with actually "$1":
    'Research.Text = Regex.Replace(R esearch.Text,
    Regex.Escape(te xtboxSearch.Tex t), "<b>$1</b>",
    RegexOptions.Ig noreCase)


    works, in finding the strings, but replaces with wrong case:
    'Research.Text = Regex.Replace(R esearch.Text,
    Regex.Escape(te xtboxSearch.Tex t), "<b>"+ textboxSearch.T ext+"</b>",
    RegexOptions.Ig noreCase)



    Thanks again.




    Comment

    • Jesse Houwing

      #3
      Re: Replacing Text without changing case??

      Hello wildman@noclien t.net,
      THANK YOU!
      >
      >Regex.Replace( string.format(" ({0})", Regex.Escape(in put)),
      >"<b>$1</b>", RegexOptions.Ig noreCase)
      >>
      Presuming
      >
      user search input : textboxSearch.t ext
      replace is : "<b>"+textboxSe arch.text+"</b>"
      data I'm looking to impact is : Research.Text (a gridview row label
      control)
      How would I use the above?
      >
      I tried:
      >
      Imports System.Text.Reg ularExpressions
      >
      Research.Text = Regex.Replace(S tring.Format("( {0})",
      Regex.Escape(te xtboxSearch.Tex t)), "<b>$1</b>",
      RegexOptions.Ig noreCase)
      >
      Getting an error on Regex.Replace : Reference to a non-shared member
      requires an object reference.
      >
      So I noticed it appears to be formated incorrectly and probably should
      be :
      >
      Research.Text = Regex.Replace(S tring.Format("( {0})"),
      Regex.Escape(te xtboxSearch.Tex t), "<b>$1</b>",
      RegexOptions.Ig noreCase)
      >
      However that did not work.
      >
      I also tried:
      >
      No effect:
      Research.Text = Regex.Replace(S tring.Format("( {0})"),
      Regex.Escape(te xtboxSearch.Tex t), "<b>$1</b>",
      RegexOptions.Ig noreCase)
      replaces string with actually "$1":
      'Research.Text = Regex.Replace(R esearch.Text,
      Regex.Escape(te xtboxSearch.Tex t), "<b>$1</b>",
      RegexOptions.Ig noreCase)
      works, in finding the strings, but replaces with wrong case:
      'Research.Text = Regex.Replace(R esearch.Text,
      Regex.Escape(te xtboxSearch.Tex t), "<b>"+ textboxSearch.T ext+"</b>",
      RegexOptions.Ig noreCase)
      Sorry, I mixed the input and the pattern parameters to the Regex.Replace
      function.

      This should do it (in C#, I'm no star in VB):

      string input = "a.b.c.d.e.f.g. h.i.j";
      string output = Regex.Replace(i nput, string.Format(" ({0})", Regex.Escape(in put)),
      "<b>$1</b>");
      Console.WriteLi ne(output);

      --
      Jesse Houwing
      jesse.houwing at sogeti.nl


      Comment

      • wildman@noclient.net

        #4
        Re: Replacing Text without changing case??

        I'm close, but this ends up highighting all of research.text when a
        match is found. How can I highlight just the found string in it's
        original case?

        Research.Text = Regex.Replace(R esearch.Text, String.Format(" ({0})",
        Regex.Escape(Re search.Text)), "<FONT style='backgrou nd-color:Yellow'>
        $1</FONT>")

        thanks.

        Comment

        • Jesse Houwing

          #5
          Re: Replacing Text without changing case??

          Hello wildman@noclien t.net,
          I'm close, but this ends up highighting all of research.text when a
          match is found. How can I highlight just the found string in it's
          original case?
          >
          Research.Text = Regex.Replace(R esearch.Text, String.Format(" ({0})",
          Regex.Escape(Re search.Text)), "<FONT style='backgrou nd-color:Yellow'>
          $1</FONT>")
          That shouldn't be too hard, but currently you're puttign the whole string
          in for both input and pattern.
          Research.Text = Regex.Replace(
          Research.Text, <== input
          String.Format(" ({0})", Regex.Escape(Re search.Text)), <== text to search
          "<FONT style='backgrou nd-color:Yellow'>$ 1</FONT>" <== replacement
          )

          So make sure you only put the text to search in the correct place.

          --
          Jesse Houwing
          jesse.houwing at sogeti.nl


          Comment

          Working...