Sebastian Zeki Sebastian Zeki - 6 months ago 13
Java Question

regex to detect the last line with floating point number or n/a

I have text as follows (using java)

HrtBrn n/a
Regurg n/a
ChestP 8.3
Cough n/a
Belch n/a
Other 0.0
Epigastric Pain n/a
Throat Burn n/a
Vomiting n/a
Stomach Pain n/a
A load of other stuff and text
HrtBrn n/a
Regurg n/a
ChestP 8.3
Cough n/a
Belch n/a
Other 0.0


but this can also be

HrtBrn n/a
Regurg n/a
ChestP 8.3
Cough n/a
Belch n/a
Other 0.0
some other text blabla
HrtBrn n/a
Regurg n/a
ChestP 8.3


It always starts with
HrtBrn
but and the end always finished with either a decimal number or n/a but the word on the final line can vary. I would like to get the chunk of text between HrtBrn and the next line that does not have either a decimal or n/a on it eg in the first example above I would like to capture

HrtBrn n/a
Regurg n/a
ChestP 8.3
Cough n/a
Belch n/a
Other 0.0
Epigastric Pain n/a
Throat Burn n/a
Vomiting n/a
Stomach Pain n/a


I am having trouble getting a capture to include the 'either decimal or n/a'- I can get up to the last decimal (so in both examples above I get up to
'Other 0.0'
) with
HrtBrn.*\d+\.(\d*|n\/a)
but I can't get the last n/a with this
Here is my regex

Answer

You can use this regex to match with DOTALL flag:

HrtBrn.*?\n[^\n]* +(?:\d+\.\d+|n/a)(?!\s+[^\n]* +(?:\d+\.\d+|n/a))

RegEx Demo

This will match till last line which ends in a decimal number or n/a.