Introduction to Regular Expressions (Regex)
Understand the syntax and core concepts of Regular Expressions for powerful text pattern matching.
Introduction to Regular Expressions (Regex) is a free DevOps Bootcamp lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What are Regular Expressions?
Regular Expressions, often shortened to Regex, are powerful patterns used for searching and manipulating text.
Think of them as super-powered search terms that can match specific sequences, characters, or even positions within text.
They are widely used in programming, scripting, and command-line tools like grep, sed, and awk in Linux.
Literal Character Matching
The simplest regex matches exact, literal characters. If you search for 'apple', it will find exactly 'apple'.
Let's use the grep command (Global Regular Expression Print) to demonstrate this basic matching.
echo "apple" > fruits.txt
echo "banana" >> fruits.txt
echo "pineapple" >> fruits.txt
grep "apple" fruits.txt
rm fruits.txtThe Dot: Any Single Character
The dot (.) is a special character that matches any single character, except for a newline.
It's like a wildcard for one position. For example, c.t would match "cat", "cot", or "cut".
echo "cat" > words.txt
echo "cot" >> words.txt
echo "cut" >> words.txt
echo "coat" >> words.txt
grep "c.t" words.txt
rm words.txtAsterisk: Zero or More (*)
The asterisk (*) is a quantifier that matches zero or more occurrences of the character or group immediately preceding it.
For instance, colou*r would match "color" (zero 'u's) and "colour" (one 'u').
echo "color" > spellings.txt
echo "colour" >> spellings.txt
echo "colouuur" >> spellings.txt
echo "cold" >> spellings.txt
grep "colou*r" spellings.txt
rm spellings.txtPlus Sign: One or More (+)
The plus sign (+) matches one or more occurrences of the preceding character or group.
Unlike *, the element must appear at least once. So, go+gle would match "google", "gooogle", but not "ggle".
Note: For +, ?, and {} to work directly with grep, you often need to use the -E option for extended regular expressions.
echo "google" > search.txt
echo "goooogle" >> search.txt
echo "ggle" >> search.txt
grep -E "go+gle" search.txt
rm search.txtQuestion Mark: Zero or One (?)
The question mark (?) matches zero or one occurrence of the preceding character or group.
It makes a character optional. For example, colou?r would match "color" (zero 'u') and "colour" (one 'u').
echo "color" >> spellings.txt
echo "colour" >> spellings.txt
echo "colouuur" >> spellings.txt
grep -E "colou?r" spellings.txt
rm spellings.txtAnchors: Start (^) and End ($)
Anchors don't match characters; they match specific positions in the text.
^matches the start of a line.$matches the end of a line.
Example: ^apple matches "apple" only if it's at the beginning of a line. apple$ matches "apple" only if it's at the end.
echo "apple pie" > menu.txt
echo "fresh apple" >> menu.txt
echo "apple" >> menu.txt
grep -E "^apple" menu.txt
grep -E "apple$" menu.txt
rm menu.txtCharacter Classes: Square Brackets ([])
Square brackets ([]) define a character class, matching any single character within the set.
[aeiou]matches any single vowel.[0-9]matches any single digit.[a-zA-Z]matches any single letter.
You can also negate a class with [^...], e.g., [^aeiou] matches any character that is NOT a vowel.
echo "cat" > animals.txt
echo "bat" >> animals.txt
echo "rat" >> animals.txt
echo "cot" >> animals.txt
grep -E "[cb]at" animals.txt
grep -E "[^cbr]at" animals.txt
rm animals.txtQuantifiers: Curly Braces ({})
Curly braces ({}) are quantifiers that specify the exact number of repetitions for the preceding character or group.
a{3}matches "aaa"a{2,}matches "aa", "aaa", "aaaa", etc. (at least 2)a{2,4}matches "aa", "aaa", "aaaa" (between 2 and 4)
Remember to use grep -E for these quantifiers.
echo "helllo" > greetings.txt
echo "hello" >> greetings.txt
echo "helo" >> greetings.txt
grep -E "hel{2,3}o" greetings.txt
rm greetings.txtRegex Quick Check
Consider these strings:
bananabanaanabanaananabna
Which regular expression would match only "banana" and "banaana"?
Regex Basics Recap
You've learned the fundamentals of Regular Expressions!
- Regex uses patterns to search and manipulate text.
- Literal characters match themselves.
- Special characters like
.,*,+,?,^,$,[], and{}provide powerful matching capabilities.
Next, you'll apply these patterns to advanced text searching with the grep command and its various options.
Frequently asked questions
Is the “Introduction to Regular Expressions (Regex)” lesson free?
Yes — the full text of “Introduction to Regular Expressions (Regex)” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.
What will I learn in “Introduction to Regular Expressions (Regex)”?
Understand the syntax and core concepts of Regular Expressions for powerful text pattern matching. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Introduction to Regular Expressions (Regex)” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Introduction to Regular Expressions (Regex)
- Advanced Grep with Regex
- Combining Regex with sed/awk
- Capture Groups, Backreferences & Substitution