Regex syntax

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • !NoItAll
    Contributor
    • May 2006
    • 297

    Regex syntax

    I'm trying to remove the following from a string
    Any occurance of

    <br> </br> <br clear="all"> <br/> <br />

    Here's my string

    <br></br><br clear="all">Hel lo bright girl<br/><br /></br>

    I am trying the .net regex like this

    Code:
    sLine = System.Text.RegularExpressions.Regex.Replace(sLine, "<\/?br.*>", String.Empty, RegularExpressions.RegexOptions.IgnoreCase)
    and it just doesn't work the way I think it should! I wind up with the following string: <b><h1>
    and that's it!
    What is wrong with my syntax?

    Here's my thinking
    < is the literal
    \/? escapes the / so I am looking for 0 or 1 / (forward slash)
    br is a literal string because I'm looking for "br"
    .* I am looking for 0 to many of any character
    > the closing literal

    I've tried all kinds of combinations - and get close...

    The following pattern works pretty well: "<\/?br\W*>"

    I get the following string:
    <b><h1><br clear="all">Hel lo bright girl</h1></b>

    For some reason, however, it misses the <br clear="all">
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Apply the same logic for your leading forward slash, that is, make the " clear='all'" optional.

    Regex: <\/?br.*?>

    Comment

    • !NoItAll
      Contributor
      • May 2006
      • 297

      #3
      Superb - thank you. I did not think you could "stack" * and ? together to accomplish this.
      I did find a site that helps a lot. It's a pretty elegant regex text application!

      RegExr is an online tool to learn, build, & test Regular Expressions (RegEx / RegExp).


      It helps because you can just play with it until you get something to work...

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Glad I could be of help.

        I prefer the desktop solutions for regex testing - there's also a good plugin for firefox :)

        Comment

        Working...