1

I have a regex which returns the correct results when I use the various on-line regex testing sites. However when I use the regex in a PowerShell script, it only returns a single line instead of multiple lines. I'm the hoping PowerShell experts here can tell me what I'm doing wrong. I'm using PowerShell v1.

This is my regex: https://regex101.com/r/eA5jB2/3

This is my powershell script:-

#Read the file 
$FilePath = "testfile.txt" 

$regex = '(?msi)^0[12][VM](?:[^\n]|\n(?!0[12][VM]|50|90))+'
get-content $FilePath | select-string -pattern $regex 

The return is a single line

01Mxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  01 01 01

instead of

01Mxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 01 01 01
01=0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
01=5xxxxxxxxxxxxxxxxxxxxxxxxxxx
0

2 Answers 2

1

The problem is in the Get-Content cmdlet, as it returns an array of lines, so you can't match the text divided by line break.

Try to use other method to get the file contents:

[IO.File]::ReadAllText($FilePath)
Get-Content $FilePath -Raw
Get-Content $FilePath | Out-String

You can find a similar question here.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but when I try the first and last methods my script returns every possible line and the second isn't supported by powershell v1. All the methods work fine when I did "get-content $filepath | out-string >temp.txt" and copied the file into the online regex test site.
@user3046742 Those methods are just for reading the file in a way that you can apply your regular expression. You still need to pipe it into Select-String.
0

I think the problem is using Select-String.

$text = 
@'
00 date
01Mxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
01 01 01 01=0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
01=5xxxxxxxxxxxxxxxxxxxxxxxxxxx
01Mxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
01 01 01 01=0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
01=9xxxxxxxxxxxxxxxxxxxxxxxxxxx
50 xxxxxxxxxxxxx xxxxxxxxxxxxxxxxx
01Vxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
01$1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
01$A xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
01$B 0xxxxxxxxxxxxxxxxxxxx
01$0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
01$5xxxxxxxxxxxxxxxxxxxxxxxxxxx
50 xxxxxxxxxxxx BatchTotal
90 xxxxxxxxxxxx FILETotal
'@ 

$regex = '(?msi)^0[12][VM](?:[^\n]|\n(?!0[12][VM]|50|90))+'


[regex]::matches($text,$regex) |
 foreach {$_.groups[0].value;'***'}

01Mxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
01 01 01 01=0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
01=5xxxxxxxxxxxxxxxxxxxxxxxxxxx
***
01Mxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
01 01 01 01=0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
01=9xxxxxxxxxxxxxxxxxxxxxxxxxxx
***
01Vxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
01$1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
01$A xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
01$B 0xxxxxxxxxxxxxxxxxxxx
01$0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
01$5xxxxxxxxxxxxxxxxxxxxxxxxxxx
***

1 Comment

That works fine! And it works when I read my file with "$text = get-content $FileName | out-string. Problem solved.

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.