Siebe Jongebloed
Siebe Jongebloed

Reputation: 4844

One regex to capture words separated by one space-character in combination with the opposite capture occurrences more than one space-character

I would like to have just one regex to capture words separated by one space-character in combination with the opposite capture occurrences more than one space-character

I would like to have the following example covered:

This line with     sometimes more than    1  space needs to be captured in 3 matches with 2 groups.

I expect the following groups:

([This line with][     ])([sometimes more than][    ])([1][  ])space needs to be captured in 3 matches with 2 groups.

To capture one of the two is no problem. i.e. to capture more than one space-char:

([\s]{2,})

and to capture words separated by only one space-char(see https://stackoverflow.com/a/60288115/3710053):

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

Upvotes: 2

Views: 723

Answers (1)

The fourth bird
The fourth bird

Reputation: 163362

You might use an alternation to match either a word followed by a repeating pattern of a single space and a word OR match 2 or more spaces

\S+(?: \S+)*| {2,}

Explanation

  • \S+ Match 1+ non whitespace chars
  • (?: \S+)* Repeat 0+ times matching a space and 1+ non whitespace chars
  • | Or
  • {2,} Repeat 2 or more times matching a space

Regex demo

If you want to match whitespace chars instead, you could replace the space with \s but note that it could also possibly match newlines.

Edit

For the updated question, you could use 2 capturing groups:

(\S+(?: \S+)*)( {2,})

Explanation

  • ( Capture group 1
    • \S+ Match 1+ non whitespace chars
    • (?: \S+)* Repeat 0+ times matching a space and 1+ non whitespace chars
  • ) Close group 1
  • ( Capture group 2
    • {2,} Match 2 or more spaces
  • ) Close group 2

Regex demo

Upvotes: 5

Related Questions