Home > Software engineering >  Get all matches in between two expressions
Get all matches in between two expressions

Time:03-16

I have some text i would like to manipulate and substitute some lines from, but I have some problems getting all the matches I need. I only want to match lines in the second block of text. Some of the numbers can change, but the GRP 200 and GRP 300 are fixed.

Sample text:

SAR - GRP 200 T 0.300000 MNO 1 MRF 2 DRX 0.000000 0.000000 -1.000000 NX -1.000000 0.000000 0.000000 H1 0.15
SARB OUT
 SLNB X1 0.00000000 -6.00000000 2.00000000 X2 0.00000000 -6.00000000 2.10000000
SARB OUT
 SLNB X1 0.00000000 -6.00000000 2.10000000 X2 0.00000000 6.00000000 2.10000000
SARB OUT
 SLNB X1 0.00000000 6.00000000 2.10000000 X2 0.00000000 6.00000000 2.00000000
SARB OUT
 SLNB X1 0.00000000 6.00000000 2.00000000 X2 0.00000000 -6.00000000 2.00000000

SAR - GRP 200 T 0.300000 MNO 1 MRF 2 DRX 0.000000 0.000000 -1.000000 NX -1.000000 0.000000 0.000000 H1 0.15
SARB OUT
 SLNB X1 0.00000000 -6.00000000 0.00000000 X2 0.00000000 -6.00000000 2.00000000
SARB OUT
 SLNB X1 0.00000000 -6.00000000 2.00000000 X2 0.00000000 6.00000000 2.00000000
SARB OUT
 SLNB X1 0.00000000 6.00000000 2.00000000 X2 0.00000000 6.00000000 0.00000000
SARB OUT
 SLNB X1 0.00000000 6.00000000 0.00000000 X2 0.00000000 -6.00000000 0.00000000

SAR - GRP 300 T 0.600000 MNO 1 MRF 2 DRX 0.000000 0.000000 -1.000000 NX 0.000000 -1.000000 0.000000 H1 0.15
SARB OUT
 SLNB X1 0.00000000 3.00000000 1.80000000 X2 0.00000000 3.00000000 0.00000000
SARB OUT
 SLNB X1 0.00000000 3.00000000 0.00000000 X2 -2.45000000 3.00000000 0.00000000
SARB OUT
 SLNB X1 -2.45000000 3.00000000 0.00000000 X2 -0.30000000 3.00000000 1.80000000
SARB OUT
 SLNB X1 -0.30000000 3.00000000 1.80000000 X2 0.00000000 3.00000000 1.80000000

SAR - GRP 300 T 0.600000 MNO 1 MRF 2 DRX 0.000000 0.000000 -1.000000 NX 0.000000 1.000000 0.000000 H1 0.15
SARB OUT
 SLNB X1 0.00000000 -3.00000000 1.80000000 X2 0.00000000 -3.00000000 0.00000000
SARB OUT
 SLNB X1 0.00000000 -3.00000000 0.00000000 X2 -2.45000000 -3.00000000 0.00000000
SARB OUT
 SLNB X1 -2.45000000 -3.00000000 0.00000000 X2 -0.30000000 -3.00000000 1.80000000
SARB OUT
 SLNB X1 -0.30000000 -3.00000000 1.80000000 X2 0.00000000 -3.00000000 1.80000000

SAR - GRP 100 T 0.400000 MNO 1 MRF 2 NX 0.000000 0.000000 1.000000 H1 0.2 CB 15000 CT 15000
SARB OUT
 SLNB X1 0.00000000 -6.00000000 0.00000000 X2 0.75000000 -6.00000000 0.00000000
SARB OUT
 SLNB X1 0.75000000 -6.00000000 0.00000000 X2 0.75000000 6.00000000 0.00000000
SARB OUT
 SLNB X1 0.75000000 6.00000000 0.00000000 X2 0.00000000 6.00000000 0.00000000
SARB OUT
 SLNB X1 0.00000000 6.00000000 0.00000000 X2 0.00000000 -6.00000000 0.00000000

I want to match the 4 lines similar to this line in the second block:

SLNB X1 0.00000000 -6.00000000 0.00000000 X2 0.00000000 -6.00000000 2.00000000 

I tried using this, but I am unable to really understand how to get all the lines from this second block:

(?:SAR - GRP 200. \s )*?(?:(?:SARB OUT)|(?<!^))*?(SLNB X1. \s )(?=SAR - GRP 300. \s )

https://regex101.com/r/442mwx/1

CodePudding user response:

I think this does what you want. A little later I may try some experiments in PowerShell to see what getting those lines look like.

I think it grabs an extra line for the last SLNB line. If that is an issue, think we can remove it.

(?s). ?SAR - GRP 200. ?SAR - GRP 200. ?(?:SARB OUT. ?(?<SLNB>\s SLNB.*?)) SAR - GRP 300.*

Let me know how close this is and what we need to change.

  • Related