Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Replace Middle of an attribute value (RegEx)?

Thread Tools

Replace Middle of an attribute value (RegEx)?

Posts: n/a

I have a string that looks like this:


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 ".")?

Reply With Quote
Jeffrey Tan[MSFT]
Posts: n/a


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)
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.
catch(Exception ex)

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! -
This posting is provided "as is" with no warranties and confers no rights.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[XSLT] Select node by attribute with value from an attribute of its self Stefan_W XML 1 11-05-2008 10:50 AM
XSLT Newbie: Attribute Value transformed to Attribute Name bprimuslevy XML 1 08-23-2006 09:56 PM
Change the value of an attribute according to the value of another attribute XML 8 08-22-2006 02:53 PM
wrapping long attribute value (line-continuation for attribute value) XML 1 08-02-2006 01:18 PM
picking value of one attribute based on a child elements attribute? XPath? XML 2 03-06-2006 05:19 AM