I am currently experimenting with basic regular expression (regex) filtering in Powershell, but I am encountering issues with the “\d” expression not working correctly. I have a directory called “Test Directory” that contains multiple files with two naming conventions: “AA###” and “AA####”. Some of these files have three digits, while others have four digits. My objective is to search for files that start with “AA” and end with exactly three digits. I am using the following command:

get-childitem -Path "$HOME\documents\Test Directory" | where {$_.Name -match "AA\d{3}"}

However, I am having trouble because the {n} quantifier in the regex expression is supposed to match exactly “n” number of times, but it seems to be searching for a minimum of three digits and possibly more, resulting in files with three or more digits being displayed. When I add the file extension “.txt” to the regex expression, such as “AA\d{3}.txt”, it works properly, but I want it to be able to search for files with the specified naming convention even when the file extension is unknown. Is there a way to explicitly limit the digit quantifier to only search for three digits and not more?

Askify Moderator Edited question April 25, 2023