prgshn
prgshn

Reputation: 53

Extracting words from a pattern

I have String s = "#stack###over##flow". How do I split s to have

String[] a = {"#", "stack", "###", "over", "##", "flow}

I tried s.split("(?<=#)|(?=#)") as in How to split a string, but also keep the delimiters? but it gives

String[] a = {"#", "stack", "#", "#", "#", "over", "#", "#", "flow}

Upvotes: 4

Views: 72

Answers (2)

user4227915
user4227915

Reputation:

I think there is a much more nice way..

Looks insane:

\b

Regex live here.

Upvotes: 2

hwnd
hwnd

Reputation: 70732

The lookarounds need to be a little more assertive, meaning that the look-behind needs to assert that the following position is either a word character or not a # and the lookahead needs to assert that the preceding position is either a word character or not a # as well.

You could use word boundaries in each alternation:

String s = "#stack###over##flow";
String[] a = s.split("(?<=#\\b)|(?=\\b#)");
System.out.println(Arrays.toString(a)); //=> [#, stack, ###, over, ##, flow]

Or modify your lookaround assertions ( longer approach ):

String[] a = s.split("(?<=#(?!#))|(?<=[^#](?=#))");

Upvotes: 5

Related Questions