الانتقال إلى المحتوى الرئيسي
eLearner.app
الوحدة 5 · الدرس 1 من 417/32 في الدورة~8 min
دروس الوحدة (1/4)

مجموعات الأحرف: `[abc]`

When a predefined class like \d or \w is not enough, you can define a custom character set with square brackets [...]. Inside the brackets, each character is an alternative: the regex matches one of any of the listed elements.

Code
Pattern: [aeiou]
Sample:  ciao mondo
         ^   ^^  ^^

Each vowel is a match on its own. Brackets match exactly one character (unless quantified): [aeiou]+ matches a contiguous sequence of vowels.

Special characters inside brackets

Inside [...] most meta-characters lose their special meaning:

  • . inside [.] matches a literal dot (no escape needed).
  • *, +, ?, (, ), {, } are literal.
  • \ ] ^ instead remain special and must be escaped or carefully positioned.
Code
Pattern: [.,;:]
Match: any punctuation mark among dot, comma, semicolon, colon.

Special characters inside square brackets

Most regex meta-characters (., *, +, ?, etc.) lose their special meaning inside brackets and are treated as literal characters. Only the dash -, the caret ^ at the start, and the backslash \\ retain operational meanings.

Try it

تمرين#regex.m5.l1.e1
المحاولات: 0جارٍ التحميل…

Find every vowel (a, e, i, o, u, including uppercase) in the text, one by one.

جارٍ تحميل المحرر…
إظهار التلميح

Add the uppercase vowels inside the brackets, or use the i flag.

الحل متاح بعد 3 من المحاولات

Review exercise

تمرين#regex.m5.l1.e2
المحاولات: 0جارٍ التحميل…

Find punctuation marks: dot, comma, semicolon, colon, exclamation mark, question mark.

جارٍ تحميل المحرر…
إظهار التلميح

Inside [.,;:!?] all characters are literal, no escaping needed.

الحل متاح بعد 3 من المحاولات

Additional challenge

تمرين#regex.m5.l1.e3
المحاولات: 0جارٍ التحميل…

Find all Italian accented vowels (`àèéìòù`) in the text.

جارٍ تحميل المحرر…
إظهار التلميح

List the accented letters inside square brackets.

الحل متاح بعد 3 من المحاولات