Regex cheat sheet and file search with PowerShell
Useful regular expressions (IPv4, empty lines, whitespace, proofreading, lookahead) and PowerShell commands to find files.
A regular expression (regex) cheat sheet for searching and cleaning text, followed by a few PowerShell commands to find files or file content.
Match an IPv4 address
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)
Filter file names
The dot is wrapped in a character class [.] so that it is literal.
| Pattern | Meaning |
|---|---|
[.]svn |
contains .svn |
[.]sql$ |
ends with .sql |
Extract.*[.]ps1$ |
starts with Extract and ends with .ps1 |
Whitespace and empty lines
| Pattern | Meaning |
|---|---|
^\n |
a simple empty line |
^(?:[\t ]*(?:\r?\n|\r))+ |
empty lines, including those containing spaces or tabs |
[ \t]+$ |
trailing spaces and tabs (replace with nothing) |
^[ ]{1,} |
leading spaces |
[ ]{2,} |
two or more spaces (replace with a single space) |
\b[ ]{2,}\b |
multiple spaces inside the text only |
(?<=\.) {2,}(?=[A-Z]) |
multiple spaces at the start of a sentence (after a period, before a capital) |
\n\n |
double line breaks (replace with \n) |
Filter a log by duration
Example filter for a log viewer (such as LogExpert) that keeps only lines whose duration is above roughly 0.8 ms:
(.*)((0\.[8-9][0-9])|([1-9]\.[0-9][0-9]))ms
Select lines by numeric prefix
Lines starting with 701, 702, 703 or 704:
^(?:70(?:1|2|3|4).*)$
Proofreading: spotting potential typos
| Pattern | Meaning |
|---|---|
\. [a-z] |
a period followed by a space and a lowercase letter |
[^\.] [A-Z] |
a capital letter that does not follow a period and a space |
[a-z] [A-Z] |
a capital letter preceded by a lowercase letter and a space |
[a-z][A-Z] |
a capital glued to a lowercase letter (accidental capital inside a word) |
[^ .][A-Z] |
a capital that follows neither a space nor a period |
\(.{5,}?\) |
parentheses with more than 4 characters (skips dates); ? makes the match non-greedy |
\d{4} |
4-digit numbers (useful for dates) |
\(\d{4}\) |
4-digit numbers in parentheses |
\w+-\w+ |
hyphenated words |
(?<=«).*(?=») |
text between French quotation marks (beware if several quotes are on the same line) |
Capitalise the first letter of each sentence (Sublime Text)
In Sublime Text, $1 refers to the first capture group and $& to the entire match. In the find/replace panel (Ctrl+H), enable regex and case sensitive, and disable "preserve case":
- Find:
(\. )([a-z]) - Replace with:
$1\U$2
\u uppercases the next character; \U uppercases everything that follows until \E.
Alternation of several values
To search for several addresses or domains in one pass, separate the values with | and put the dot in [.]:
user1@example[.]com|user2@example[.]org|@example[.]net
Find files with PowerShell
Search by name across a drive:
Get-ChildItem -Path C:\ -Include *Report* -File -Recurse -ErrorAction SilentlyContinue
With exclusions:
Get-ChildItem -Path C:\ -Include *Report* -Exclude *.JPG,*.MP3,*.TMP -File -Recurse -ErrorAction SilentlyContinue
Filter the name with a regex:
Get-ChildItem | Where-Object { $_.Name -match '^KB[0-9]+\.log$' }
Search inside file content:
Get-ChildItem c:\*.txt -Recurse | Select-String -Pattern EX
Get-ChildItem c:\*.txt -Recurse | Select-String -Pattern "EX.*E"
List the base names of .js files, largest first, and copy them to the clipboard:
Get-ChildItem -Path .\ -Filter *.js -Recurse -File | Sort-Object Length -Descending | ForEach-Object {
$_.BaseName
} | clip
Match a line containing two words in any order
With lookahead assertions, each condition is tested independently:
^(?=.*\bjack\b)(?=.*\bjames\b).*$
This approach makes it easy to add more conditions:
^(?=.*\bjack\b)(?=.*\bjames\b)(?=.*\bjason\b)(?=.*\bjules\b).*$