Mike
Mike

Reputation: 87

Can someone explain how does this regular expression behave?

String [] numbers =s.split("(?<=\\G.{50})");

I know what split is, but why do I need [], what do those do? And most importantly, can someone explain "(?<=\\G.{50})" thoroughly?

Upvotes: 0

Views: 72

Answers (1)

ziesemer
ziesemer

Reputation: 28687

The returned array will contain one String for each result returned by the split function, for any matches returned after separating the input string on the provided regular expression.

This regular expression provided here is making use of zero-width positive lookbehinds, as documented at https://docs.oracle.com/javase/8/docs/api/index.html?java/util/regex/Pattern.html . It is searching for anything that comes BEFORE the end of the previous match (\G - escaped with another \ as a Java String literal), followed by any 50 characters.

In short - this is just splitting your input of s into 50-character chunks. (Not sure I would have used a Regular Expression for this - but it works...)

Upvotes: 2

Related Questions