Regex match all whitespace before character

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ceestand
    New Member
    • Mar 2007
    • 8

    #1

    Regex match all whitespace before character

    I'm trying to remove all whitepace in an HTML-formatted string, except within the actual content.

    so I want to get:
    Code:
    <p>   <b>some text</b> </p>
    to look like
    Code:
    <p><b>some text</b></p>
    I've been trying something like:
    Code:
    Regex r = new Regex(@"/s<);
    r.Replace(str, "<");
    with the idea being to replace all the whitespace immediately preceeding the "<" character. That would be fine for what I'm doing, but no expression using *,+,.,?, etc, is working. The best I'm able to get is removing the first whitespace before an "<" character, but sometimes there's more than one.

    Please help, the only Regex expressions I'm coming up with right now are "@$*%!!!"
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    @(>){ }*(<b)$ or
    @(/b>){ }*(<)$

    Should be somewhat like that.

    the first one @(>){ }*(<b)$ checks that part of the string that starts with >
    and ends with <b with spaces in between.

    the second part @(/b>){ }*(<)$ finds the string that starts with /b> and ends with <

    My regex is pretty shady at the moment...so @ represents the string that has to match starts with the charecters specified after that and $ repreents the chrachter which should contain at the end of the string being searched / matched for.

    Hope this helps.

    Comment

    Working...