[Top level Bash Page] .... [References]
Regular Expressions
This page serves to guide us as we learn regular expressions. References [3,4,7,23] seem to be the best source of help.
Install the check-regexp program (apt-get install source-highlight) - this is a very helpful tool. Give it a regexp and a text string and it shows not only if the regexp works as expected but provides further information as to how regexp might work.
- Remember, shell metachararcters are expanded before the shell passes arguements so regex patterns must be in quotes to prevent the shell from interpretting
- Structure of regex
- Anchors- used to specify the position of the pattern : ^, $, \b \B \< \>
- character sets - match one or more characters in a single position : [ ] with any of the following: A-Z, a-z, 0-9, :predefined in current local: such as alnum, alpha, digit, ...
- modifiers - specify how many times the prev. character can be repeated : . , ?, *, +, {n,m}
- backrerferences: \n where n is a digit that points to the n'th parenthesized subexpr
- Basic and Extended regex
- Basic supported by vi/sed/grep /more
- Extended supported by awk/egrep/
Regular expressions examples
- "^#*" this is: to match 0 or more "#" character but ONLY if it starts at the beginning of a line
- Let's say we needed to find lines in a file that :
- Start with a T
- Is the first word on a line
- Second letter is lower case
- Exactly three letters long
- Third letter is a vowel
- /^T[a-z][aeiou]/
Last update: 3/22/2017