Menguasai Ekspresi Reguler (Regex)
Pahami sintaks dan kekuatan ekspresi reguler untuk pencocokan pola tingkat lanjut.
Menguasai Ekspresi Reguler (Regex) adalah pelajaran Linux Command Line Mastery gratis di CoddyKit. Ini adalah pelajaran 1 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Linux Command Line Mastery, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Linux Command Line Mastery mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
Intro to Regular Expressions
Welcome to Regular Expressions (Regex)! Regex is a powerful mini-language used for pattern matching in text. It's like a super-charged search tool.
You can use Regex to search, replace, and validate specific text patterns in files or command output. Mastering it will make you much more efficient at the command line!
Matching Exact Text
The simplest Regex matches literal characters. This means the pattern will look for the exact sequence of characters you provide.
For example, searching for apple will only find occurrences of "apple".
echo "I have an apple and a pineapple." | grep 'apple'Any Single Character: The Dot (.)
The dot (.) is a special character, called a "metacharacter". It matches any single character, except for a newline. Use it when you don't care what character is in a specific spot.
echo "cat, cot, cut, c@t" | grep 'c.t'Matching Repetition: Quantifiers
Quantifiers let you specify how many times a character or group should appear. These are powerful for flexible matching:
*: Matches zero or more occurrences.+: Matches one or more occurrences.?: Matches zero or one occurrence (makes it optional).
echo "color colour coor" | grep 'colou*r'Specific Choices: Character Classes
Use square brackets ([]) to match any one character from a specified set. This defines specific options for a single position in your pattern.
For example, [aeiou] would match any single vowel.
echo "grey gray" | grep 'gr[ae]y'Defining Ranges in Classes
Inside character classes, you can specify ranges of characters using a hyphen (-). This makes your patterns much shorter and easier to read.
[0-9]: Matches any digit.[a-z]: Matches any lowercase letter.[A-Z]: Matches any uppercase letter.[a-zA-Z]: Matches any letter (case-insensitive).
echo "num1 num2 numA" | grep 'num[0-9]'Excluding Characters: Negation
To match any character that is NOT in a specified set, place a caret (^) as the first character inside the square brackets ([^...]).
echo "apple, banana, 123, !@#" | grep '[^a-z]'Start and End: Anchors
Anchors don't match characters; they match positions. They "anchor" your pattern to the start or end of a line.
^: Matches the beginning of a line.$: Matches the end of a line.
Note: When ^ is used outside of [], it's an anchor, not a negation!
echo -e "start here\n here end\nstart and end" | grep '^start'Putting It All Together
Let's combine some of these concepts. Imagine you want to find lines that start with a capital letter, followed by any number of lowercase letters, and end with a digit.
echo -e "Hello1\nworld2\nAnotherTest3" | grep '^[A-Z][a-z]*[0-9]$'Regex Challenge
Consider the following text:
apple
apples
apply
app
AppleWhich regular expression would match "apple" and "apples", but NOT "apply", "app", or "Apple"?
Recap: Regex Fundamentals
In this lesson, you've taken your first steps into the powerful world of Regular Expressions! We covered:
- Literal matching for exact text.
- The
.metacharacter for any single character. - Quantifiers (
*,+,?) for repetition. - Character classes (
[]) and ranges ([a-z]) for specific sets. - Negation (
[^]) to exclude characters. - Anchors (
^,$) for start and end of lines.
These are the building blocks for creating sophisticated text patterns. Keep practicing!
Belajar Linux Command Line Mastery dengan tutor AI — gratis
Tulis dan jalankan kode asli di browser kamu, dapatkan bantuan instan dari tutor AI 24/7, dan lanjutkan di mana kamu tinggalkan di web atau aplikasi.
- Kursus
- 12
- Pelajaran
- 48
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Menguasai Ekspresi Reguler (Regex)” gratis?
Ya — teks lengkap “Menguasai Ekspresi Reguler (Regex)” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Linux Command Line Mastery, upgrade ke CoddyKit PRO. Kursus Linux Command Line Mastery mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “Menguasai Ekspresi Reguler (Regex)”?
Pahami sintaks dan kekuatan ekspresi reguler untuk pencocokan pola tingkat lanjut. Kamu berlatih Linux Command Line Mastery dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai Linux Command Line Mastery?
Tidak diperlukan pengalaman sebelumnya. Linux Command Line Mastery di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 1 dari 4.
Berapa lama pelajaran “Menguasai Ekspresi Reguler (Regex)” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran Linux Command Line Mastery ini?
Ya. Setiap pelajaran Linux Command Line Mastery menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- Menguasai Ekspresi Reguler (Regex)
- Pola Lanjutan `grep` dan `find`
- `xargs` untuk Penggabungan Perintah
- Mengedit Stream dalam Skala Besar dengan sed dan tr