Take the 2-minute tour ×

How can I replace part of a string that has a potentially unknown starting index. For instance, if I had the following strings:

"<sometexthere width='200'>"
"<sometexthere tile='test' width='345'>"

I would be looking to replace the width attibute value that can have an unknown value and as mentioned before an unknown start index.

The I understand that I would somehow have to base this on the following part, which is constant, I just don't quite understand how to achieve this.

share|improve this question
This looks like a job for ... Regular Expressions! –  jrummell Mar 7 '12 at 21:08
+1 for finding creative way to bypass standard answers for "how to parse HTML with RegEx" and "I want to parse and construct XML with string manipulations". –  Alexei Levenkov Mar 7 '12 at 21:17
@jrummell: This looks like a job for a parser. This does not look like a job for regular expressions. First off, regular expressions do not take into account the grammar of the markup, and second every regular expression posted here so far is wrong. –  Eric Lippert Mar 7 '12 at 21:51
No, this looks like a job for ... HtmlAgilityPack –  Brian Mar 7 '12 at 21:52
@EricLippert I concede to your wisdom. RegEx match open tags except XHTML self-contained tags was very enlightening. –  jrummell Mar 7 '12 at 21:57

8 Answers 8

up vote 2 down vote accepted
using System.Text.RegularExpressions;
Regex reg = new Regex(@"width='\d*'");
string newString = reg.Replace(oldString,"width='325'");

This will return a new string with a new width, provided you put a number between the ' ' in the new width field.

share|improve this answer
Nice clean solution Jetti, thanks for that. +1 –  Ryan Smith Mar 7 '12 at 21:20
@RyanSmith Glad I could help! –  Jetti Mar 7 '12 at 21:27

So far you've got seven answers telling you to do the wrong thing. Do not use regular expressions to do the job of a parser. I am assuming that your string is a hunk of markup. Let's suppose it is HTML. What does your regular expression do with:

    var width='100';
<blah width =
... and so on ...

I'd be willing to bet as much as a dollar that it replaces the JScript code, which it should not, and does not replace the attribute of the blah tag -- it is perfectly legal to have whitespace in an attribute.

If you have to parse a markup language then parse the markup language. Get yourself a parser and use it; that's what parsers are for.

share|improve this answer
+1. THIS. (more characters) –  Rob Mar 8 '12 at 6:40
I'm glad someone mentioned this. I thought the exact same thing, when I read the other answers. –  cypressious Mar 10 '12 at 19:44

Take a look at the Regex class, you can search for the content of the attribute and repalce the value with this class.

Off the cuff Regex.Replace might do the trick:

var newString = Regex.Replace(@".*width='\d'",string.Foramt("width='{0}'",newValue));
share|improve this answer
Please see: stackoverflow.com/questions/1732348/… –  Khan Mar 7 '12 at 21:09

Use the regular expression

Regex regex = new Regex(@"\b(width)\b\s*=\s*'d+'");

where the \bs indicate that you wish to match a whole word, \s* allows for zero or any number of whitespace charaters and \d+ allows for one or more numeric placeholder. To replace the numeric value you can then use:

int nRepValue = 400;
string strYourXML = "<sometexthere width='200'>";

// Does the string contain the width?
string strNewString = String.Empty;
Match match = regex.Match(strYourXML);
if (match.Success)
    strNewString = 
        regex.Replace(strYourXML, String.Format("match='{0}'", nRepValue.ToString()));
    // Do something else...

Hope this helps.

share|improve this answer
Why one whitespace character? XML allows for unbounded whitespace. –  Eric Lippert Mar 7 '12 at 21:49
Amended - I am not sure why I did that :]. Thanks. –  Killercam Mar 8 '12 at 0:37

You can use a regular expression (RegEx) to find and replace all the text in single quotes after "width=".

share|improve this answer

You could use a regular expression, like (?<=width=')(\d+)


var replaced = Regex.Replace("<sometexthere width='200'>", "(?<=width=')(\\d+)", "123");"

replaced is now: <sometexthere width='123'>

share|improve this answer

I would use a Regex.
Something like this to replace the width value with 123456.

string aString = "<sometexthere tile='test' width='345'>";
Regex regex = new Regex("(?<part1>.*width=')(?<part2>\\d+)(?<part3>'.*)");
var replacedString = regex.Replace(aString, "${part1}123456${part3}");
share|improve this answer

Use regular expressions to achieve this:

using System.Text.RegularExpressions;


string yourString = "<sometexthere width='200'>";

// updates width value to 300
yourString = Regex.Replace(yourString , "width='[^']+'", width='300');

// replaces width value with height value of 450
yourString = Regex.Replace(yourString , "width='[^']+'", height='450');
share|improve this answer
Assuming the width attribute is always a numeric value. Not necessarily a valid assumption. –  Jacob Proffitt Mar 7 '12 at 21:13

Your Answer


