Skip to content

whaleLogic/Regex-Reference-Guide

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 

Repository files navigation

Shell Scripting Cheat Sheet

See python-specific-regex.md for python-specific identifiers.
Category Syntax / Command Description
Text Manipulationecho "Hello, World!"Prints text to the terminal.
Text Manipulationprintf "Name: %s\n" "Alice"Formatted output.
Arithmeticexpr 5 + 3Basic arithmetic.
VariablesVAR="value"Assigns a value to a variable.
Loopsfor i in {1..5}; do echo $i; doneFor loop.
Control Flowif [ "$x" -eq 10 ]; then echo "Yes"; fiIf statement.
Functionsmy_func() { echo "Hello"; }Defines a function.
File Operationstouch file.txtCreates an empty file.
Process Managementps auxLists running processes.
Redirectionscommand > file.txtRedirects output to a file.
Pipescommand1 | command2Pipes output of one command to another.

PCRE (Perl-Compatible Regular Expressions) Cheat Sheet

Pattern Description Example
.Matches any single character (except newline)./a.c/ matches "abc", "adc", "a#c"
\dMatches any digit (0-9)./\d+/ matches "123", "42"
\DMatches any non-digit./\D+/ matches "abc", "hello"
\wMatches any word character (a-z, A-Z, 0-9, _)./\w+/ matches "hello_123"
\WMatches any non-word character./\W+/ matches "!!!", "@@@"
\sMatches any whitespace (space, tab, newline)./\s+/ matches spaces
\SMatches any non-whitespace character./\S+/ matches "word"
^Matches the start of a line./^abc/ matches "abc" at start
$Matches the end of a line./abc$/ matches "abc" at end
[abc]Matches any character inside brackets./[aeiou]/ matches vowels
[^abc]Matches any character NOT in brackets./[^0-9]/ matches non-digits
(...)Capturing group./(hello)/ captures "hello"
|Logical OR./cat|dog/ matches "cat" or "dog"
*Matches 0 or more occurrences./a*/ matches "", "a", "aaa"
+Matches 1 or more occurrences./a+/ matches "a", "aaa"
?Matches 0 or 1 occurrences./a?/ matches "", "a"
{n}Matches exactly n times./a{3}/ matches "aaa"
{n,}Matches n or more times./a{2,}/ matches "aa", "aaa"
{n,m}Matches between n and m times./a{2,4}/ matches "aa", "aaa", "aaaa"

Common Regex identifiers

Regex Description Example
. Matches any single character except a newline (\n) h.t matches hat, hit, but not h/t.
^ Anchors to the start of a string or line ^Hello matches "Hello" at the beginning of a string.
$ Anchors to the end of a string or line world$ matches "world" at the end of a string.
\ Escape character for special symbols \. matches a literal .
[] Character class, matches any single character within [aeiou] matches any vowel.
[^] Negated character class, matches any single character not within [^aeiou] matches any non-vowel.
| Alternation (logical OR)
() Capturing group; groups patterns and captures the match for later reference (ab)+ matches "ab", "abab", etc.
(?: ) Non-capturing group; groups patterns without capturing them (?:ab)+ matches "abab" but doesn’t capture it.
{} Quantifier specifying exact or range repetitions a{3} matches "aaa". a{2,4} matches "aa", "aaa", or "aaaa".
* Matches 0 or more of the preceding character or group a* matches "", "a", "aa", etc.
+ Matches 1 or more of the preceding character or group a+ matches "a", "aa", etc., but not "".
? Matches 0 or 1 of the preceding character or group; makes quantifiers lazy when placed after a? matches "", or "a". a*? matches as few "a"s as possible.
/ Delimiter for regex in some languages like JavaScript or Perl /hello/ matches "hello".
\d Matches any digit (0-9) \d{2} matches "12".
\D Matches any non-digit \D matches "a", but not "2".
\w Matches any word character (alphanumeric + underscore) \w+ matches "word123".
\W Matches any non-word character \W matches "@", but not "a".
\s Matches any whitespace character (spaces, tabs, newlines) \s+ matches spaces between words.
\S Matches any non-whitespace character \S+ matches "word123", but not " ".
\b Matches a word boundary \bword\b matches "word", but not "sword".
\B Matches a position that is not a word boundary \Bword matches "sword", but not " word".

About

Reference table of regular expression identifiers for PCRE, Python, POSIX, BASH etc.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published