Master Regular Expressions: From Basic grep to Advanced egrep Techniques
This article introduces regular expressions, explains the difference between basic (grep) and extended (egrep) syntax, lists common meta‑characters, quantifiers, anchors, grouping, and shows practical command‑line examples with grep options for text searching and manipulation.
What is a regular expression?
Regular expressions (regex) are patterns used to match strings, allowing special characters (meta‑characters) to control matching, substitution, and more. They are supported by many programming languages and text editors for searching and manipulating text.
Classification of regular expressions
Based on the number and functionality of meta‑characters, regexes are divided into Basic Regular Expressions (BRE, used by grep) and Extended Regular Expressions (ERE, used by egrep).
Basic Regular Expressions (grep)
Syntax: grep [option]… 'PATTERN' FILE… Meta‑characters for character matching:
. # any single character
[ ] # any single character from the set
[0-9] # a digit
[a-z] # a lowercase letter
[A-Z] # an uppercase letter
[[:digit:]] # a digit (POSIX class)
[[:lower:]] # a lowercase letter
[[:upper:]] # an uppercase letter
[[:space:]] # a whitespace character
[[:punct:]] # a punctuation character
[[:alnum:]] # a letter or digit
[[:alpha:]] # a letter (case‑insensitive)
[^] # any character not in the setQuantifiers:
* # zero or more of the preceding element
\? # zero or one
\{m\} # exactly m times
\{m,n\} # between m and n times
\{m,\} # at least m times
\{0,n\} # at most n times
.* # any length of any charactersAnchors:
^ # start of line
$ # end of line
^$ # empty line
\<b # word boundary (start)
\>b # word boundary (end)Grouping and back‑references:
( ) # group
# reference to the n‑th captured groupExtended Regular Expressions (egrep)
Syntax: egrep [option] 'PATTERN' FILE… Meta‑characters for character matching are similar, with | for alternation.
. # any single character
[ ] # any character from the set
[^] # any character not in the setQuantifiers, anchors, grouping, and back‑references follow the same principles, with + meaning “one or more”.
Common grep/egrep options
-v # invert match
-o # show only matching part
-i # ignore case
-A # show N lines after match
-B # show N lines before match
-C # show N lines around matchPractical examples
Various command‑line examples illustrate how to use grep and egrep to find lines in files, filter by patterns, extract numbers, and more.
The End
These notes summarize common regular‑expression usage; they are personal study notes and may contain omissions.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
