Currently I have a regex that will take a given set of newlines and condense them. A challenge I need to solve for is to modify this regex (\\n{2,}
) so that it
This answer ensures whitespace at the start of a line is kept (if it contains something other than a whitespace character).
See regex in use here
(?:\h*\n){2,}
Note: Some regex engines don't allow \h
, so this will have to be replaced with [\t\p{Zs}]
, and if Unicode character classes are not supported, a simple list of each character such as [\t ]
or [^\S\n]
.
Other methods:
(?:\n(?:[^\S\n]*(?=\n))?){2,}
(?:\n(?:\s*(?=\n))?){2,}
\h*\n(?:\h*\n)+
**Language**
- Added four languages: Italian, Portuguese (Brazil), Spanish (Mexico) and Chinese (Traditional)
**Bug fixes**
- Fixed camera jittering for passenger sitting on the back of a motorcycle with sidecar
- Fixed camera jittering for passenger sitting on the back of a motorcycle with sidecar
**Language**
- Added four languages: Italian, Portuguese (Brazil), Spanish (Mexico) and Chinese (Traditional)
**Bug fixes**
- Fixed camera jittering for passenger sitting on the back of a motorcycle with sidecar
- Fixed camera jittering for passenger sitting on the back of a motorcycle with sidecar
(?:\h*\n){2,}
Match any number of horizontal whitespace character followed by a line-feed, two or more timesJust to explain at least one of the other methods (and keep my original answer)
(?:\n(?:[^\S\n]*(?=\n))?){2,}
Matches the following two or more times
\n
Match a line-feed character(?:[^\S\n]*(?=\n))?
Match the following zero or one time
[^\S\n]*
Match any whitespace character except \n
any number of times(?=\n)
Positive lookahead ensuring what follows is a line-feed \n
This method is shorter than the top answer:
(\h*\n){2,}
Regex101