Capturing seperators with regex

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

    Capturing seperators with regex

    I have the following regular expression: (\s+-\s+|\s+\/\s+|\s+\\\s+|
    \t)

    Is there a better way to write this? I am looking to capture the
    following characters - / \ and tab. There can be 0, 1 or more space
    before and after these seperators.

    Right now it captures these characters if there is a 1 or more spaces
    before and after each seperator. However, there are instances where
    there will be 0 spaces before or after or both before and after.

    Thanks
  • Paul E Collins

    #2
    Re: Capturing seperators with regex

    "Nightcrawl er" <thomas.zaleski @gmail.comwrote :
    I have the following regular expression:
    (\s+-\s+|\s+\/\s+|\s+\\\s+|\t )
    >
    Is there a better way to write this? I am looking to capture the
    following characters - / \ and tab. There can be 0, 1 or more space
    before and after these seperators.
    If you want 0, 1 or more (instead of just one or more) you should use *
    instead of +. In a regular expression, * means "zero or more" and +
    means "one or more".

    Eq.


    Comment

    • Ben Voigt [C++ MVP]

      #3
      Re: Capturing seperators with regex

      Nightcrawler wrote:
      I have the following regular expression: (\s+-\s+|\s+\/\s+|\s+\\\s+|
      \t)
      >
      Is there a better way to write this? I am looking to capture the
      following characters - / \ and tab. There can be 0, 1 or more space
      before and after these seperators.
      How about this?
      "\\s*(?:[-/\\t]\s*)+"
      >
      Right now it captures these characters if there is a 1 or more spaces
      before and after each seperator. However, there are instances where
      there will be 0 spaces before or after or both before and after.
      >
      Thanks

      Comment

      • Nightcrawler

        #4
        Re: Capturing seperators with regex

        Thanks! That helped!

        Comment

        Working...