Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Replace Middle of an attribute value (RegEx)?

 
 
localhost
Guest
Posts: n/a
 
      12-17-2003

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.
 
Reply With Quote
 
 
 
 
Jeffrey Tan[MSFT]
Guest
Posts: n/a
 
      12-18-2003

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.doc Form.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.

 
Reply With Quote
 
 
 
Reply

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 patrizio.trinchini@googlemail.com XML 8 08-22-2006 02:53 PM
wrapping long attribute value (line-continuation for attribute value) lophiomys@gmx.at XML 1 08-02-2006 01:18 PM
picking value of one attribute based on a child elements attribute? XPath? vjethava@gmail.com XML 2 03-06-2006 05:19 AM



Advertisments