ریجیکس بنیادی چیٹ شیٹ

ریجولر ایکسپریشن (regex) ایک پیٹرن ہوتا ہے جو ٹیکسٹ سے ملتا ہے۔ ہر سمبل، جسے ٹوکن کہتے ہیں، پیٹرن کے رویے کو بدلتا ہے۔ نیچے کی شیٹ سب سے کارآمد ٹوکن، ان کا سادہ مطلب اور کاپی کرنے کے قابل چھوٹا سا مثال دیتی ہے۔

اپ ڈیٹ:

ٹوکنمطلبمثال
.Matches any single character except newline.a.c matches abc, a c
*Matches the preceding token zero or more times.ab* matches a, abb
+Matches the preceding token one or more times.ab+ matches ab, abb
?Makes the preceding token optional (zero or one).colou?r matches color, colour
[]Character set: matches any one character inside.[aeiou] matches any vowel
[^]Negated set: matches any character NOT inside.[^0-9] matches any non-digit
\dMatches any digit (0-9).\d+ matches 42, 007
\wMatches any word character (a-z, A-Z, 0-9, _).\w+ matches hello_1
\sMatches any whitespace (space, tab, newline).a\sb matches a b
^Anchors the match to the start of a line/string.^Hello matches start of string
$Anchors the match to the end of a line/string.world$ matches end of string
|Alternation: matches the pattern on either side.cat|dog matches cat or dog
()Groups a sub-pattern (and captures it).(ab)+ matches ab, abab
{n,m}Quantifier: match the preceding token n to m times.a{2,4} matches aa, aaaa
(?i)Inline flag: makes the pattern case-insensitive.(?i)hello matches HELLO

نوٹس

اکثر پوچھے گئے سوالات

ڈاٹ (.) کس سے ملتا ہے؟
ڈیفالٹ طور پر ڈاٹ نیو لائن کے علاوہ کسی بھی ایک کیریکٹر سے ملتا ہے۔ لفظی ڈاٹ کے لیے اسے \ سے ايسکیپ کریں۔
* اور + میں کیا فرق ہے؟
اسٹار * پچھلے ٹوکن سے صفر یا زیادہ بار ملتا ہے، اس لیے 'ab*' 'a' سے بھی ملتا ہے۔ پلس + کم از کم ایک بار مانگتا ہے، 'ab' سے ملتا ہے لیکن صرف 'a' سے نہیں۔
ہندسے یا ورڈ کیریکٹر سے کیسے ملیں؟
کسی بھی ہندسے (0-9) کے لیے \d اور ورڈ کیریکٹر (حروف، ہندسے، انڈر اسکور) کے لیے \w استعمال کریں۔ انکار \D اور \W ہیں۔
^ اور $ کے لیے کیا ہیں؟
^ میچ کو سٹرنگ یا لائن کے شروع میں اور $ اختتام میں باندھتا ہے۔ اکٹھے ^pattern$ پوری سٹرنگ کا میل کرنا لازمی بناتا ہے۔