Mister B
Mister B

Reputation: 123

Separate regex groups

I have a java regex to get a (numeric space text) value but when i have a new line the value doesn´t grouped, sample

regex expression (\d{14}\s.*\n)

Value :

59105999000321 My value start
with new line number
12105999000343 AAAAAasasas asdasdasd
32105999000323 asdasxasxasx asdasd

the group result in https://regex101.com/ is:

Match 1
Full match  0-29    `59105999000321 My value start`
Group 1.    0-29    `59105999000321 My value start`
Match 2
Full match  51-87   `12105999000343 AAAAAasasas asdasdasd`
Group 1.    51-87   `12105999000343 AAAAAasasas asdasdasd`
Match 3
Full match  88-122  `32105999000323 asdasxasxasx asdasd`
Group 1.    88-122  `32105999000323 asdasxasxasx asdasd`

I have a regex with following result:

group 1 : 59105999000321 My value start
    with new line number

group 2: 12105999000343 AAAAAasasas asdasdasd

group 3: 32105999000323 asdasxasxasx asdasd

I expect

group 1 : 59105999000321 My value start with new line number 

group 2: 12105999000343 AAAAAasasas asdasdasd 

group 3: 32105999000323 asdasxasxasx asdasd

how i do this in java?

Upvotes: 0

Views: 55

Answers (1)

Matt.G
Matt.G

Reputation: 3609

Try Regex: (\d{14}\s.*(?:\n*(?!^\d{14}).*)*)

Demo

Explanation: Since the requirement is to match lines starting with 14 digits, a negative lookahead for the same is added after \n*

Upvotes: 1

Related Questions