Regex -- Replace "-*?\n" with ""

Discussion in 'ASP .Net' started by Brent, Jul 22, 2005.

  1. Brent

    Brent Guest

    Take this string:

    "----------------------------------------
    "

    (i.e., hyphens followed by a newline )

    I thought I could match it simply with this Regex:

    "-*?\n"

    (my interpretation: one or more of "-" followed by a newline)

    But when I run it, it seems to match all newlines, regardless of being
    preceded by the "-". I know I'm missing something. Any ideas of how to
    do this match properly?

    Many thanks.

    --Brent
    Brent, Jul 22, 2005
    #1
    1. Advertising

  2. Brent

    Guest

    Hi Brent,

    Starting a newline is \r\n on dos/windows systems.

    So if you use
    "-*?\r\n"
    on
    "----------------------------------------
    "
    "----------------------------------------
    "

    It will match 2 times.

    A caution is maybe in order here. If you use a lazy non greedy quantifier
    it will match the shortest string possible.

    So if you let the regex
    -*?
    Loose on 5 times a dash
    -----
    The result will be 6 *empty* matches : m-m-m-m-m-m

    Whe empty? bacause the star in your regex allows 0 occurences. So the shortest
    string possible is the empty string.

    Compare this with the regex
    -*?a
    on
    -----a

    Now you want an a to be at the end of a match so the shortest string possible
    is now : -----a
    This is the whole original input string and this is the only match.

    You say you want a windows newline so your doing fine.


    Let me know if you have any more questions..

    Cheers,
    Tom Pester

    > Take this string:
    >
    > "----------------------------------------
    > "
    > (i.e., hyphens followed by a newline )
    >
    > I thought I could match it simply with this Regex:
    >
    > "-*?\n"
    >
    > (my interpretation: one or more of "-" followed by a newline)
    >
    > But when I run it, it seems to match all newlines, regardless of being
    > preceded by the "-". I know I'm missing something. Any ideas of how to
    > do this match properly?
    >
    > Many thanks.
    >
    > --Brent
    >
    , Jul 22, 2005
    #2
    1. Advertising

  3. Brent

    Brent Guest

    Thanks Tom!

    I hacked around some more with this stuff, and it turns out that
    ".*?--+\n" works. Some of the "----" strings were preceded by spaces, so
    this seems to be a better, uh, match.

    And, yes, your reply does help.

    Oh...the text is being sucked in over HTTP directly from an Apache
    server, so the \n seems to be appropriate.

    Tricky things, these regexes!

    --Brent


    wrote:
    > Hi Brent,
    >
    > Starting a newline is \r\n on dos/windows systems.
    >
    > So if you use
    > "-*?\r\n"
    > on
    > "----------------------------------------
    > "
    > "----------------------------------------
    > "
    >
    > It will match 2 times.
    >
    > A caution is maybe in order here. If you use a lazy non greedy
    > quantifier it will match the shortest string possible.
    >
    > So if you let the regex -*?
    > Loose on 5 times a dash
    > -----
    > The result will be 6 *empty* matches : m-m-m-m-m-m
    >
    > Whe empty? bacause the star in your regex allows 0 occurences. So the
    > shortest string possible is the empty string.
    >
    > Compare this with the regex
    > -*?a
    > on
    > -----a
    >
    > Now you want an a to be at the end of a match so the shortest string
    > possible is now : -----a
    > This is the whole original input string and this is the only match.
    >
    > You say you want a windows newline so your doing fine.
    >
    >
    > Let me know if you have any more questions..
    >
    > Cheers,
    > Tom Pester
    Brent, Jul 23, 2005
    #3
  4. Brent

    Bruce Barker Guest

    actually http 1.1 switched to "\r\n", so the apache server should serve the
    html the samer way.

    -- bruce (sqlwork.com)



    "Brent" <""b b i g l e r \"@ y a h o o . c o m"> wrote in message
    news:...
    > Thanks Tom!
    >
    > I hacked around some more with this stuff, and it turns out that
    > ".*?--+\n" works. Some of the "----" strings were preceded by spaces, so
    > this seems to be a better, uh, match.
    >
    > And, yes, your reply does help.
    >
    > Oh...the text is being sucked in over HTTP directly from an Apache server,
    > so the \n seems to be appropriate.
    >
    > Tricky things, these regexes!
    >
    > --Brent
    >
    >
    > wrote:
    >> Hi Brent,
    >>
    >> Starting a newline is \r\n on dos/windows systems.
    >>
    >> So if you use
    >> "-*?\r\n"
    >> on
    >> "----------------------------------------
    >> "
    >> "----------------------------------------
    >> "
    >>
    >> It will match 2 times.
    >>
    >> A caution is maybe in order here. If you use a lazy non greedy quantifier
    >> it will match the shortest string possible.
    >>
    >> So if you let the regex -*?
    >> Loose on 5 times a dash
    >> -----
    >> The result will be 6 *empty* matches : m-m-m-m-m-m
    >>
    >> Whe empty? bacause the star in your regex allows 0 occurences. So the
    >> shortest string possible is the empty string.
    >>
    >> Compare this with the regex
    >> -*?a
    >> on
    >> -----a
    >>
    >> Now you want an a to be at the end of a match so the shortest string
    >> possible is now : -----a
    >> This is the whole original input string and this is the only match.
    >>
    >> You say you want a windows newline so your doing fine.
    >>
    >>
    >> Let me know if you have any more questions..
    >>
    >> Cheers,
    >> Tom Pester
    Bruce Barker, Jul 23, 2005
    #4
  5. Brent

    jasonkester Guest

    Try:

    -+\r\n

    Not sure what you were trying to accomplish with your *?
    (literally:zero or more, possibly.) That would always match any new
    line, assuming you used \r or \r\n as your newline character.


    Jason Kester
    Expat Software Consulting Services
    http://www.expatsoftware.com/
    jasonkester, Jul 23, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Blais
    Replies:
    1
    Views:
    376
    Bruno Desthuilliers
    Jun 27, 2006
  2. Greg Ewing
    Replies:
    2
    Views:
    344
    Dieter Maurer
    Jun 29, 2006
  3. Alun
    Replies:
    3
    Views:
    4,507
    Masudur
    Feb 18, 2008
  4. Replies:
    3
    Views:
    758
    Reedick, Andrew
    Jul 1, 2008
  5. Prasad S
    Replies:
    2
    Views:
    229
    Dr John Stockton
    Aug 27, 2004
Loading...

Share This Page