Replace Middle of an attribute value (RegEx)?

Discussion in 'ASP .Net Web Controls' started by localhost, Dec 17, 2003.

  1. localhost

    localhost Guest

    I have a string that looks like this:

    "init();this.docForm.textBox1.focus
    ();this.docForm.textBox1.select();"

    In this case, I need to replace "textBox1"
    with "textBox2". However, the value-to-replace may not
    be "textBox1", it could be anything. What kind of RegEx
    would I need to replace a value between other parts of a
    string (in this case between "docForm." and a ".")?

    Thanks.
    localhost, Dec 17, 2003
    #1
    1. Advertising

  2. Hi

    Based on my understanding, you want to look for all the occurrence of
    string between "docForm." and ".", then for each occurrence string, replace
    it with "textBox2".
    I think you can do like this:

    private void button1_Click(object sender, System.EventArgs e)
    {
    try
    {
    string
    str="init();this.docForm.textBox1.focus();this.docForm.textBox1.select();";
    Regex regex=new Regex("docForm.([^.]*)."); //note: the [^.]
    standard for the string can not contain ".", () means I make a groups
    collection for the string between "docForm." and "."
    MatchCollection matches=regex.Matches(str); //find all the
    occurrence of Regular expression
    string newstr=str;
    foreach(Match match in matches) //foreach
    occurrence replace it.
    {
    Regex newregex=new Regex(match.Groups[1].Value);
    newstr=newregex.Replace(newstr,"textBox2",1); //the third
    parameter 1 means for one occurrence, we only replace once.
    }
    MessageBox.Show(newstr);
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    }

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
    Jeffrey Tan[MSFT], Dec 18, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    4,264
    Joe Kesselman
    Aug 2, 2006
  2. Replies:
    8
    Views:
    521
  3. Replies:
    3
    Views:
    746
    Reedick, Andrew
    Jul 1, 2008
  4. Raimon Fs
    Replies:
    10
    Views:
    328
    Raimon Fs
    Apr 7, 2009
  5. Sherm Pendley

    need to negate regex in middle of expression

    Sherm Pendley, Jun 20, 2005, in forum: Perl Misc
    Replies:
    8
    Views:
    155
    Tad McClellan
    Jun 20, 2005
Loading...

Share This Page