regex (lookahead) with multiple digits

Tomakin :

I have a String which looks like:

text = "9) text of 9\r\n10) text of 10\r\n11) text of 11\r\n12) ...\r\n123) text of 123"

I am trying to split it up as follows:

String[] result = text.split("(?=\\d+\\))");

The result I am looking for is:

- result[0] = "9) text of 9"
- result[1] = "10) text of 10"
- result[2] = "11) text of 11"
- ...

But it is not working. Which regex should I use in conjunction with text.split() ?

B4NewWebDev :

I think you were very close - did you try adding the delimiter prior to the lookahead?

String[] result = text.split("\\r\\n(?=\d+\))");

I tried it in the JS console (the JS regex is pretty similar to the Java regex processor)

let x= "9) text of 9 10) text of 10 11) text of 11 ... 123) text of 123"
let result = x.split(/\\r\\n(?=\d+\))/);

result then gives the array you wanted

Update: Updated code answer based on updated question

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=327397&siteId=1