3

Regex gurus, please help me here:

I need a regex that finds more than one space which does not end in a newline. For example consider the text below:

Column 1       Column 2       Column 3        Column 4
Column 1       Column 2       Column 3        Column 4

Regex should match the spaces between Column X, where X = 1, 2 or 3, but should not match the space after Column 4. Regex should also not match the single space between the word "Column" and its respective number.

I have tried \s+^(\n) but it is not working

Klaus Nji
  • 18,107
  • 29
  • 105
  • 185
  • What you need is a [lookbehind](http://www.regular-expressions.info/lookaround.html) – Antarr Byrd Dec 12 '14 at 16:57
  • How about: `[ \t]+(?!\n)` – Wolph Dec 12 '14 at 16:57
  • 3
    @AvinashRaj not the same question. The question you refer to is looking for any white space not consecutive. – Antarr Byrd Dec 12 '14 at 17:01
  • you could easily modify the regexes to match two or more spaces. – Avinash Raj Dec 12 '14 at 17:02
  • @klaus `I need a regex that finds more than one space which does not end in a newline` What do you mean by this? – Avinash Raj Dec 12 '14 at 17:04
  • `Column X, where X = 1, 2 or 3, but should not match the space after Column 4` but there isn't a single horizontal space exists after to the col 4. Did you mean the `\n` exist next to the col4? – Avinash Raj Dec 12 '14 at 17:06
  • 2
    This is not a duplicate. It's not about the spaces themselves, but a sequence of spaces not ending with a newline. @AvinashRaj please remove this flag and I can answer it. – Oved D Dec 12 '14 at 17:14
  • @OvedD i wondered, that why op fail to make a conversation with me. See his example, there isn't a horizontal space exits next to the column 4. – Avinash Raj Dec 12 '14 at 17:20
  • @OvedD According to your wish :) – Avinash Raj Dec 12 '14 at 17:22
  • @AvinashRaj, this is not a duplicate. None of the answers in the post you've suggested as duplicate works in Notepad++. Also, there could be 1 more spaces after column 4. I do not want to match those, since they will end with a newline. – Klaus Nji Dec 12 '14 at 18:22
  • @anubhava, I am currently working in Notepad++. – Klaus Nji Dec 12 '14 at 18:25
  • @Wolph, your regex is matching white space between the word "Column" and its respective number e.g space in "Column 4". – Klaus Nji Dec 12 '14 at 18:27
  • @KlausNji: ah, didn't know you wanted to ignore those. In that case it's `{2,}` instead of `+` like this: `[ \t]{2,}(?!\n)` – Wolph Dec 12 '14 at 18:49

2 Answers2

4

You can use this regex:

/ {2,}(?! *$)/gm

RegEx Demo

Explanation:

  • {2,} - matches 2 or more spaces
  • }(?! *$) - is a negative lookahead to make sure to not to match when there or only 0 or more spaces before end of input
  • m flag makes sure that every newline is matched by anchor $ (used above in lookahead)
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 2
    Perhaps add an explanation of the working parts rather than just a regex? *That* would merit an upvote – RobP Dec 12 '14 at 17:26
  • Question was put on hold as soon as I posted an answer. However I have added it now. – anubhava Dec 12 '14 at 17:29
0

This works:

\b(\s(?![\n\r])){2,}\b

It essentially says match:

  • whole "words", hence the \b word boundary
  • with two or more spaces \s {2,}
  • not followed by a return character, by using a negative lookbehind (?![\n\r])
Oved D
  • 7,132
  • 10
  • 47
  • 69