0Pricing
NLP Academy · 课时

5 分钟掌握正则表达式

字面量、字符类与量词

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

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

A Tiny Language for Patterns

A regular expression is a tiny pattern language for matching text. Instead of one fixed word, you describe the shape of what you want to find.

Python Speaks Regex Too

Python uses the built-in re module for regex. Import it once and you can search, match, and extract patterns from any string.

import re
re.search("cat", "the cat sat")

Literals: Exact Characters

The simplest pattern is a literal. The pattern dog matches the exact letters d, o, g, in that order, wherever they appear in your text.

search Finds the First Match

Use re.search to scan a string and return the first place your pattern appears, or None when nothing matches at all.

m = re.search("sat", "the cat sat")
print(m.group())

Character Classes With Brackets

A character class in square brackets matches any one listed character. The pattern [abc] matches a single a, b, or c each time.

re.findall("[aeiou]", "hello")

Handy Class Shortcuts

Some classes have shortcuts. \d matches any digit, \w matches word characters, and \s matches spaces. They save you a lot of typing. 😊

The Dot Matches Anything

The dot stands for any single character except a newline. So h.t matches hat, hit, and hot, but never the two-letter ht.

Quantifiers Repeat Patterns

A quantifier says how many times the part before it repeats. Add a plus for one or more, a star for zero or more.

re.findall("a+", "aaa banana")

Exact Counts With Braces

Use braces for precise repetition. The pattern \d{3} matches exactly three digits in a row, perfect for area codes or short IDs.

re.findall("\d{3}", "call 911 now")

Anchors Pin the Position

Anchors match a position, not a character. The caret means start of string and the dollar sign means end of string.

findall Returns Every Match

While search finds one match, re.findall returns a list of every match in the text, which is great for counting or extracting many items.

re.findall("\d+", "2 cats, 5 dogs")

Quick Check

You want to match exactly three digits in a row. Which pattern fits?

Recap: Your Regex Toolkit

You met literals, classes, the dot, quantifiers, and anchors. With re.search and re.findall, you can already describe and find real patterns. 🎉

常见问题解答

「5 分钟掌握正则表达式」课时是免费的吗?

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

「5 分钟掌握正则表达式」这节课中我会学到什么?

字面量、字符类与量词 你通过在浏览器中直接运行的动手代码来练习 NLP Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 NLP Academy 需要有经验吗?

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

「5 分钟掌握正则表达式」课时需要多长时间?

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

我能在这节 NLP Academy 课中编写并运行代码吗?

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

此课程中的所有课时

  1. 5 分钟掌握正则表达式
  2. 查找电子邮件地址与网址
  3. 捕获组与替换
  4. 正则表达式分词技巧
← 返回 NLP Academy