LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

regex

Regular expression syntax reference

TLDR

This is a reference page for regular expression syntax, not a standalone command.

SYNOPSIS

regex — regular expression pattern matching syntax

DESCRIPTION

Regular expressions (regex) are patterns used to match character combinations in strings. They are supported by many commands including grep, sed, awk, perl, and programming languages.

BASIC REGEX (BRE)

.: Match any single character\*: Match zero or more of preceding element^: Match start of line$: Match end of line[abc]: Match any character in brackets[^abc]: Match any character not in brackets[a-z]: Match character range\\: Escape special character

EXTENDED REGEX (ERE)

+: Match one or more of preceding element?: Match zero or one of preceding element|: Alternation (OR)(...): Grouping{n}: Match exactly n times{n,}: Match n or more times{n,m}: Match between n and m times

CHARACTER CLASSES

\d: Digit (PCRE) — equivalent to [0-9]\w: Word character — [a-zA-Z0-9_]\s: Whitespace — [ \t\n\r\f]\D, \W, \S: Negated versions

POSIX CLASSES

[[:alpha:]]: Alphabetic characters[[:digit:]]: Digits[[:alnum:]]: Alphanumeric[[:space:]]: Whitespace[[:upper:]]: Uppercase letters[[:lower:]]: Lowercase letters[[:punct:]]: Punctuation characters[[:print:]]: Printable characters (including space)[[:blank:]]: Space and tab

ANCHORS

^: Start of line/string$: End of line/string\b: Word boundary (PCRE)\B: Non-word boundary (PCRE)

CAVEATS

Different tools support different regex flavors: BRE (basic), ERE (extended), PCRE (Perl-compatible). Use appropriate flags: grep -E for ERE, grep -P for PCRE.Escape sequences and metacharacters vary between flavors. Test patterns with your specific tool.Greedy vs. non-greedy: \* and + are greedy by default. Use **\*? and +?** for non-greedy matching (PCRE).

SEE ALSO

grep(1), sed(1), awk(1), perlre(1)

Braincup
Open source brain training for math, memory and focus
Braincup mini-games
41 mini-games · Apache-2.0
No ads · No tracking
Play in browser
Download Braincup on the App StoreGet Braincup on Google PlayGet Braincup on F-Droid
276 stars
From the maker of Linux Command Library
Copied to clipboard
Braincup
Open source brain training for math, memory and focus. 41 mini-games, from mental arithmetic to Sudoku, N-Back and Solo Chess.
Apache-2.0 licensed · No ads · No tracking · No account
From the maker of Linux Command Library
Download Braincup on the App StoreGet Braincup on Google PlayGet Braincup on F-Droid