Regex for replacing all consecutive dashes to single dash

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DotNetNewbie

    Regex for replacing all consecutive dashes to single dash

    Hi,

    I want to replace all consecutive (2 or more) dashes into a single
    dash.

    So text like "hello--world is-cool------ok?"

    should become "hello-word is-cool-ok?"

    so it doesn't effect anything except replace all consecutive dashes
    into a single dash.

    What would the regex look like?
  • Martin Honnen

    #2
    Re: Regex for replacing all consecutive dashes to single dash

    DotNetNewbie wrote:
    I want to replace all consecutive (2 or more) dashes into a single
    dash.
    See
    <URL:http://groups.google.c om/group/microsoft.publi c.dotnet.langua ges.vb/browse_thread/thread/6959b1e85a7ea69 e/5604494b6830ccf b?lnk=st&q=#560 4494b6830ccfb>,
    code there is VB.NET but the regular expression obviously is the same
    for all .NET languages.


    --

    Martin Honnen --- MVP XML

    Comment

    • Luc E. Mistiaen

      #3
      Re: Regex for replacing all consecutive dashes to single dash

      something like: Text = Regex.Replace(T ext, "-{2,}", "-") ;

      /LM

      "DotNetNewb ie" <snowman908070@ yahoo.comwrote in message
      news:e3ca82c2-a4c2-4cf4-b5a5-dcf926b5ef41@60 g2000hsy.google groups.com...
      Hi,
      >
      I want to replace all consecutive (2 or more) dashes into a single
      dash.
      >
      So text like "hello--world is-cool------ok?"
      >
      should become "hello-word is-cool-ok?"
      >
      so it doesn't effect anything except replace all consecutive dashes
      into a single dash.
      >
      What would the regex look like?

      Comment

      Working...