Home > database >  Regex splitting by colon up until a word containing a colon java
Regex splitting by colon up until a word containing a colon java

Time:08-09

I am trying to split between commands denoted with colons in regex.

I think it's best to explain using an example:

Given the string:

alfa bravo charlie:delta echo foxtrot golf:hotel india juliett: kilo

I want to match the following sections:

charlie:delta echo foxtrot golf:hotel india and juliett: kilo

I am able to match the first one with:

(\w :.*?)\s \w :.* and I figure there is someway to check for the final one using $ but so far I haven't figured out the trick. If this isn't clear please comment

CodePudding user response:

If you only want to match word characters and whitespace character, you could write the pattern as:

\w :\s*\w (?:\s \w )*(?=\s \w :|$)

Explanation

  • \w :\s*\w Match 1 word chars, : optional whitespace chars and 1 word chars
  • (?:\s \w )* Optionally repeat 1 whitespace chars and 1 word chars
  • (?= Positive lookahead, assert what is to the right is
    • \s \w : Match 1 whitespace chars followed by 1 word chars and :
    • | Or
    • $ Assert end of string
  • ) Close lookahead

In Java

String regex = "\\w :\\s*\\w (?:\\s \\w )*(?=\\s \\w :|$)";

Regex demo

A bit more broader variant:

[^\s:] :.*?(?=\s*[^\s:] :|$)

Regex demo

  • Related