Maximum length of regex pattern to Select-String?

Viewed 463

Is there a maximum length of the text to Select-String -Pattern parameter?

I have a list of ~500 words (database table names) that are joined into a single string. The string is ~12,500 characters in length.

'TABLE1|TABLE2|TABLE3|...

I do not have any failure message, but can I trust that this would work?

2 Answers

Here the -Pattern parameter of Select-String accepts a string[].

-Pattern <string[]>

    Required?                    true
    Position?                    0
    Accept pipeline input?       false
    Parameter set name           (All)
    Aliases                      None
    Dynamic?                     false

Now the Maximum length of a string is 2147483647 bytes. You can get the max capacity of string by:

[text.stringbuilder]::new().MaxCapacity

So that means the maximum length of Select-String RegEx can be 2147483647 bytes (But it is most likely that you will reach out of memory before this point ;-)

In addition to the theory, here's a test that can give you some reassurance.

First we simply create a text file with 26 rows each containing 1 character from the English alphabet repeated 40,000 times.

$(
97..122 | foreach {"$([char]$_)" * 40000}
) | out-file c:\temp\longstringtest.txt -Encoding utf8

Now we'll test finding a string with 20,000 b characters.

$pattern = "b" * 20000

If(Select-String -path C:\temp\longstringtest.txt -Pattern $pattern){"Found it!"}

Found it!

We can go ahead and bump it up to 40,000

$pattern = "b" * 40000

If(Select-String -path C:\temp\longstringtest.txt -Pattern $pattern){"Found it!"}

Found it!

Just to be sure it's really working

$pattern = "ba" * 40000

If(Select-String -path C:\temp\longstringtest.txt -Pattern $pattern){"Found it!"} # No output

I wasn't brave enough to test the maximum size listed by Wasif, but I did confirm up to 200,000 worked fine as well.

Related