0

I am using regex to return a portion of a file. This is what the bit of file looks like:

L1*4***13258
S5*1*CL*9587*L*756*CA*1603*E
G62*10*20190124*Y*1210*LT
NTE*GEN*Pallets: FLOORLOADED
N1*SF*FLOWERS BAKING CO OF LAKELAND, INC.
N3*3355 WEST MEMORIAL BLVD
N4*LAKELAND*FL*33815*US
OID*HAI-60657-20190121*HAI-60657**CA*756*L*9587
S5*2*CU*9587*L*756*CA*1603*E
G62*68*20190125*Z*1200*LT
NTE*GEN*Pallets: FLOORLOADED
N1*ST*HAINES CITY
N3*2651 HWY 17
N4*HAINES CITY*FL*33844*US
OID*HAI-60657-20190121*HAI-60657**CA*756*L*9587
L3*9587*G*********756*L

And I want to return everything from the first appearance of 'S5' to the first appearance of 'L3'. The regex I am using now gets the correct selection, but returns it all in one line. This is the code:

$pattern = "S5(.*?)L3"
$string = Get-Content $file.FullName 
$result = [regex]::match($string, $pattern).Groups[1].Value 

$result

And when I run it, I get:

*1*CL*9587*L*756*CA*1603*E G62*10*20190124*Y*1210*LT NTE*GEN*Pallets: FLOORLOADED N1*SF*FLOWERS BAKING CO OF LAKELAND, INC. N3*3355 WEST MEMORIAL BLVD N4*LAKELAND*FL*33815*US OID*HAI-60657-20190121*HAI-60657**CA*756*L*9587 S5*2*CU*9587*L*756*CA*1603*E G62*68*20190125*Z*1200*LT NTE*GEN*Pallets: FLOORLOADED N1*ST*HAINES CITY N3*2651 HWY 17 N4*HAINES CITY*FL*33844*US OID*HAI-60657-20190121*HAI-60657**CA*756*L*9587 

Which is the correct feedback I'm looking for, but unfortunately it doesn't seem to respect new lines and all comes in as one line.

Does anyone have any advice to get it to preserve the new lines?

Thanks very much.

0

1 Answer 1

2

2 things you'll want to change:

In order to have Get-Content return one big multi-line string, use the -Raw switch parameter:

$string = Get-Content $file.FullName -Raw

Now, since we have a multi-line string, we'll need to instruct the regex engine to match newline characters when using . - we can do this with the s regex option:

$pattern = "(?s)S5(.*?)L3"

Now this will work as expected:

$result = [regex]::Match($string, $pattern).Groups[1].Value 
Sign up to request clarification or add additional context in comments.

2 Comments

This is definitely the correct answer! Thank you so much for your help, I was getting truly frustrated! I thought that 's' was used for whitespace, meaning spaces only.
\s is whitespace, (?s) at the beginning is something else though, see learn.microsoft.com/en-us/dotnet/standard/base-types/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.