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='
5
  • 3
    This looks like a job for ... Regular Expressions! Commented Mar 7, 2012 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". Commented Mar 7, 2012 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. Commented Mar 7, 2012 at 21:51
  • No, this looks like a job for ... HtmlAgilityPack Commented Mar 7, 2012 at 21:52
  • 2
    @EricLippert I concede to your wisdom. RegEx match open tags except XHTML self-contained tags was very enlightening. Commented Mar 7, 2012 at 21:57

8 Answers 8

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm glad someone mentioned this. I thought the exact same thing, when I read the other answers.
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));

1 Comment

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.

Comments

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.

2 Comments

Why one whitespace character? XML allows for unbounded whitespace.
Amended - I am not sure why I did that :]. Thanks.
0

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

Comments

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'>

Comments

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}");

Comments

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');

1 Comment

Assuming the width attribute is always a numeric value. Not necessarily a valid assumption.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.