Naryoril
Naryoril

Reputation: 601

Regex for a string that contains anything but consecutive spaces

I think it's easiest to start off with an example line

3432543 name that % might 7 include pretty . much 433 anything 545231 4522

I'm looking for an expression that matches the name that % might 7 include pretty . much 433 anything portion so that it includes everything until it encounters 2 spaces, and will not match if it had to include 2 or more spaces. So for example I want this pattern

^\d+ +(pattern) +\d+$

to end up not matching, and not ending up including the 545231 as part of the name. Please keep in mind that this is just a simple example to illustrate the problem, it will be included in much more complex expressions matching more complex strings.

Upvotes: 1

Views: 142

Answers (1)

anubhava
anubhava

Reputation: 785246

You may use this regex to match substring that is only single space separated:

\S+(?:\s\S+)*

RegEx Demo

This match will start with 1+ non-whitespace text followed 0+ of such words separated by a single space only.

For your desired pattern match use:

^\d+\s+\S+(?:\s\S+)*\s+\d+$

Upvotes: 2

Related Questions