3

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.

width='
Ryan Smith
  • 475
  • 1
  • 8
  • 19
  • 3
    This looks like a job for ... [Regular Expressions](http://www.regular-expressions.info/examples.html)! – jrummell Mar 07 '12 at 21:08
  • 3
    +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 07 '12 at 21:17
  • 11
    @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 07 '12 at 21:51
  • No, this looks like a job for ... [HtmlAgilityPack](http://htmlagilitypack.codeplex.com/) – Brian Mar 07 '12 at 21:52
  • 2
    @EricLippert I concede to your wisdom. [RegEx match open tags except XHTML self-contained tags](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) was very enlightening. – jrummell Mar 07 '12 at 21:57

8 Answers8

35

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:

<html>
<script>
    var width='100';
</script>
<blah width =
              '200'>
... 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.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
4

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));
jrummell
  • 42,637
  • 17
  • 112
  • 171
Pete Stensønes
  • 5,595
  • 2
  • 39
  • 62
  • 6
    Please see: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Khan Mar 07 '12 at 21:09
2
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.

Jetti
  • 2,418
  • 1
  • 17
  • 25
2

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()));
else 
    // Do something else...

Hope this helps.

MoonKnight
  • 23,214
  • 40
  • 145
  • 277
0

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

Scorpion-Prince
  • 3,574
  • 3
  • 18
  • 24
0

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

Example:

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

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

Andre Loker
  • 8,368
  • 1
  • 23
  • 36
0

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');
robyaw
  • 2,274
  • 2
  • 22
  • 29
0

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}");
Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108