0Pricing
NLP Academy · レッスン

キャプチャグループと置換

一致した部分を取り出して書き換える

「キャプチャグループと置換」はCoddyKit上の無料NLP Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはNLP Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 NLP Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Grab Parts of a Match

A capturing group wraps part of a pattern in parentheses so you can pull out just that piece, not the whole match.

re.search("(\d+)-(\d+)", "12-34")

Reading Groups by Number

After a match, group(1) returns the first parenthesized part, group(2) the second, and group(0) the entire matched string.

m.group(1)  # first group

Many Matches With Groups

When you use findall with groups, each result becomes a tuple of the captured parts, making structured data easy to collect.

re.findall("(\w+)=(\w+)", "a=1 b=2")

Name Your Groups

You can give a group a name for clearer code. Reach it later with group by that name instead of remembering a number.

re.search("(?P<year>\d{4})", "2026")

Swap Text With re.sub

The re.sub function finds every match and replaces it with text you choose. It is search and replace, powered by patterns.

re.sub("\d+", "#", "a1 b22")

Reuse Captures in Replacements

Inside a replacement you can reference a captured group with \1. This lets you reorder or rewrite matched text on the fly.

re.sub("(\w+) (\w+)", "\2 \1", "hi there")

Redact Sensitive Data

A common use of re.sub is redaction. Match phone numbers or emails, then replace each one with a placeholder like a row of stars.

Replace With a Function

For tricky edits, pass a function to re.sub. It receives each match and returns the replacement, so you can compute results per match.

Non-Capturing Groups

Sometimes you group only to apply a quantifier, not to capture. A non-capturing group starts with ?: so it never clutters your results.

Count Your Replacements

Use re.subn instead of re.sub to get back both the new string and a count of how many replacements it actually made.

re.subn("a", "X", "banana")

Groups Make Data Structured

Capturing groups turn raw text into structured fields. A date string becomes year, month, and day you can store or compare directly.

Quick Check

You matched a full date and want only the year part you wrapped in parentheses. How do you read it?

Recap: Capture and Rewrite

You learned capturing groups, named groups, and re.sub. Together they let you extract precise fields and rewrite text in one clean pass. ✨

よくある質問

「キャプチャグループと置換」レッスンは無料ですか?

はい。「キャプチャグループと置換」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、NLP Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 NLP Academyコースには全4レッスンが含まれています。

「キャプチャグループと置換」で何を学びますか?

一致した部分を取り出して書き換える ブラウザで直接実行するハンズオンコードでNLP Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

NLP Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのNLP Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「キャプチャグループと置換」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このNLP Academyレッスンでコードを書いて実行できますか?

はい。すべてのNLP Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 5 分でわかる正規表現
  2. メールアドレスと URL を見つける
  3. キャプチャグループと置換
  4. 正規表現によるトークン化のテクニック
← NLP Academyに戻る