0

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?

azro
  • 53,056
  • 7
  • 34
  • 70
Mister B
  • 123
  • 2
  • 15

1 Answers1

1

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*

Matt.G
  • 3,586
  • 2
  • 10
  • 23
  • Good, but, I need separarate by groups and the infinite \n until another 14 digits or the last line, is possible? – Mister B May 31 '18 at 11:00
  • please edit the Demo link with your failed cases.(After adding the strings, use the update regex link in the top left). also, could you provide more details as to what you mean by "I need separate by groups"?. Full Match and Group 1 are giving matches as you have mentioned in your post. – Matt.G May 31 '18 at 11:12
  • The Edited value https://regex101.com/r/B2hqjR/2 I need separate matcher 14 digits until another 14 digits or the end of text Text value ================= 59105999000321 My value start with new line number aaaa 12105999000343 AAAAAasasas 32105999000323 asdasxasxasx asdasd weqpowepoqwiq ============ The first match 59105999000321 My value start with new line number aaaa the second match 12105999000343 AAAAAasasas the third match 32105999000323 asdasxasxasx asdasd weqpowepoqwiq --------------------- Do you understood? – Mister B May 31 '18 at 11:30
  • 1
    see if `(\d{14}\s.*(?:\n*(?!^\d{14}).*)*)` works. [Demo](https://regex101.com/r/B2hqjR/3) – Matt.G May 31 '18 at 12:02
  • @MisterB, I have edited the answer with the regex in the last comment – Matt.G May 31 '18 at 17:17