Skip to content

Latest commit

 

History

History
56 lines (50 loc) · 3.41 KB

regex.md

File metadata and controls

56 lines (50 loc) · 3.41 KB

Regular Expressions in Bash

Bash uses the Extended Regular Expression (ERE) dialect. Regular Expression patterns that use capturing groups (parentheses) will have their captured strings assigned to the BASH_REMATCH array variable for later retrieval.

Symbol Meaning
. Any character except newline
^ Start of line. When within brackets, negates the match
$ End of line
? Preceding item will be matched zero or one times
* Preceding item will be matched zero or more times
+ Preceding item will be matched one or more times
{n} Preceding item is matched exactly n times
{n,} Preceding item is matched n or more times
{,m} Preceding item is matched at most m times
{n,m} Preceding item is matched at least n times,but not more than m times
\b Word boundary
\B Not word boundary
\< Start of word
\> End of word
\w Word character, same as [[:alnum:]]
\W Not word character, same as [^[:alnum:]]
\\ Literal \
[abq] Range (a or b or q)
[^abq] Not a or b or q
[a-q] Letters between a and q, inclusive
[]ab] match literal ] or a or b. Place ] first
[ab^] a or b or literal ^. Place ^ anywhere but first
[aq-] a or q or literal -. Place - last.
(a|b) a or b
(...) Group
\n Backreference, ie \1 matches first group

Note: most meta-characters lose their special meaning inside brackets

POSIX Character Classes

Symbol Meaning
[:alnum:] Digits and letters
[:alpha:] All letters
[:cntrl:] Control characters
[:digit:] Digits
[:graph:] Printed characters
[:lower:] Lower case letters
[:print:] Printed characters and spaces
[:punct:] Punctuation
[:space:] Blank characters
[:upper:] Upper case letters
[:xdigit:] Hexadecimal digits

Note: In actual usage these character classes will be inside brackets,thus: [[:alpha:]] but could also be [[:alpha:]7] match a letter or 7