Clean Text Files with Regular Expressions Revisited
In the November 2022 Ask Jeff article, I shared a simple regular expression pattern you could use to filter out blank and commented lines, using a # character, from text files.
Get-Content .\myBackupPaths.txt | where {$_ -notmatch "^(#|$)"} | Test-Path
The pattern is looking to match any line with characters that doesn’t start with a # symbol. However, the thing about regular expressions is that you have to have some idea about what the data looks like. You can’t build a pattern without knowing what you are matching. One of my readers was having issues with my pattern and test data. He provided an alternative pattern.
This is where learning regular expressions is worth your time because there is almost always more than one way to construct a pattern. Your pattern can be as broad or as granular as you need. My original pattern was trying to be broad but would fail with spaces around the # character.