مجموعات الالتقاط والاستبدالات
استخرج الأجزاء المتطابقة وأعد صياغتها
مجموعات الالتقاط والاستبدالات درس مجاني في NLP Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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 groupMany 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/7) وفتح باقي دورة NLP Academy، انتقل إلى CoddyKit PRO. تتضمن دورة NLP Academy 4 دروس في المجموع.
ماذا ستتعلم في «مجموعات الالتقاط والاستبدالات»؟
استخرج الأجزاء المتطابقة وأعد صياغتها تتمرن على NLP Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ NLP Academy؟
لا تُشترط خبرة سابقة. NLP Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «مجموعات الالتقاط والاستبدالات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس NLP Academy هذا؟
نعم. كل درس في NLP Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- التعبيرات النمطية في 5 دقائق
- العثور على عناوين البريد الإلكتروني وعناوين URL
- مجموعات الالتقاط والاستبدالات
- حيل تقسيم النص بالـ Regex