Fundamentals 7 min read

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.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Regular Expressions: From Basic grep to Advanced egrep Techniques

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 set

Quantifiers:

*   # 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 characters

Anchors:

^   # 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 group

Extended 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 set

Quantifiers, 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 match

Practical 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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

regular expressionsGrepregex-tutorialegrep
MaGe Linux Operations
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.