#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Regex replace html tags


    The following isn't replacing the html tags.

    string input = sourceCode;
    string pattern = "<tr[^>]*?><td[^>]*?></td></tr><tr[^>]*?><td[^>]*?></td></tr>";
    string replacement = "<tr[^>]*?><td[^>]*?><br /></td></tr>";
    Regex rgx = new Regex(pattern);
    string htmlResult = rgx.Replace(input, replacement);
  2. #2
  3. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    Perhaps you could show an example of the input and expected output.
    I don't think you're allowing for any data between the tags - is this intentional?

    I was playing with:
    Replace: <tr[^>]*?><td[^>]*>([^<]*?)</td>
    With: <tr><th>$1</th>

    Which (seems to) replace the first pair of <td> with <th>, keeping the text between.

    But I'm not an expert and don't know what language you are using..
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Location
    Katy, Texas
    Posts
    495
    Rep Power
    199
    It seems you would want to code
    Code:
     
    string input = sourceCode;
    string pattern = "<tr([^>]*?)><td([^>]*?)></td></tr><tr[^>]*?><td[^>]*?></td></tr>";
    string replacement = "<tr$1><td$2><br /></td></tr>";
    Regex rgx = new Regex(pattern);
    string htmlResult = rgx.Replace(input, replacement);
    Do you agree? Disagree? And remember, it's all about the reputation power...
  6. #4
  7. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,898
    Rep Power
    3887
    Doing anything HTML-y with regexps is generally not a good idea - there are HTML parsers available for pretty much any language, so that would be the best way to deal with this sort of thing.

    In any event, the replacement string will just be a string, so you wouldn't use regexp constructs like [^>]*? in it.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0
    Originally Posted by ishnid
    Doing anything HTML-y with regexps is generally not a good idea - there are HTML parsers available for pretty much any language, so that would be the best way to deal with this sort of thing.

    In any event, the replacement string will just be a string, so you wouldn't use regexp constructs like [^>]*? in it.

    I tried each suggestion but it didn't work. It's the same.
    I'm working with the source code string.
  10. #6
  11. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,898
    Rep Power
    3887
    Originally Posted by l9429727
    I tried each suggestion but it didn't work. It's the same.
    You need to provide more information. You haven't said what programming language you're using, or what you're trying to replace the tags with.

IMN logo majestic logo threadwatch logo seochat tools logo