0Pricing
Linux Command Line Mastery · 课时

掌握正则表达式(Regex)

了解正则表达式的语法和强大功能,用于高级模式匹配。

掌握正则表达式(Regex) 是 CoddyKit 上的免费 Linux Command Line Mastery 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Linux Command Line Mastery 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Linux Command Line Mastery 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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 Apple

Which 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!

常见问题解答

「掌握正则表达式(Regex)」课时是免费的吗?

是的 — 「掌握正则表达式(Regex)」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Linux Command Line Mastery 课程的其余内容,请升级到 CoddyKit PRO。 Linux Command Line Mastery 课程共包含 4 节课。

「掌握正则表达式(Regex)」这节课中我会学到什么?

了解正则表达式的语法和强大功能,用于高级模式匹配。 你通过在浏览器中直接运行的动手代码来练习 Linux Command Line Mastery,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Linux Command Line Mastery 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Linux Command Line Mastery 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「掌握正则表达式(Regex)」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Linux Command Line Mastery 课中编写并运行代码吗?

能。每节 Linux Command Line Mastery 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 掌握正则表达式(Regex)
  2. 高级 `grep` 和 `find` 模式
  3. 使用 `xargs` 链接命令
  4. 使用 sed 与 tr 进行大规模流编辑
← 返回 Linux Command Line Mastery