Regex পরীক্ষক
স্ট্রিং-এ রেগুলার এক্সপ্রেশন পরীক্ষা করুন এবং সমস্ত ম্যাচ হাইলাইট দেখুন
প্যাটার্ন
পরীক্ষার স্ট্রিং
রেগুলার এক্সপ্রেশন কী?
রেগুলার এক্সপ্রেশন (regex বা regexp) হলো অক্ষরের একটি ক্রম যা একটি সার্চ প্যাটার্ন সংজ্ঞায়িত করে। Regex পরীক্ষক আপনাকে একটি প্যাটার্ন লিখতে, নমুনা টেক্সটে প্রয়োগ করতে এবং রিয়েল টাইমে সমস্ত ম্যাচ হাইলাইট দেখতে দেয়। এই ধারণার উৎপত্তি গণিতবিদ Stephen Kleene-এর ১৯৫০-এর দশকের রেগুলার ল্যাঙ্গুয়েজ গবেষণা থেকে। Ken Thompson ১৯৬৮ সালে QED টেক্সট এডিটরে প্রথম regex ইঞ্জিন তৈরি করেন।
একটি regex ইঞ্জিন প্যাটার্ন বাঁ থেকে ডানে পড়ে এবং ম্যাচ করার চেষ্টায় ইনপুট অক্ষর গ্রহণ করে। আংশিক ম্যাচ ব্যর্থ হলে ইঞ্জিন ব্যাকট্র্যাকিং ব্যবহার করে: এটি পিছিয়ে আসে এবং প্যাটার্নের মধ্য দিয়ে বিকল্প পথ চেষ্টা করে। কিছু ইঞ্জিন (যেমন Go-তে ব্যবহৃত RE2) প্যাটার্নকে deterministic finite automata (DFA)-তে রূপান্তর করে ব্যাকট্র্যাকিং সম্পূর্ণ এড়িয়ে যায়, যা রৈখিক সময়ে ম্যাচিং নিশ্চিত করে — তবে back-reference-এর মতো বৈশিষ্ট্যের জন্য সমর্থন হারায়।
Regex সিনট্যাক্স আলগাভাবে মানকীকৃত। PCRE (Perl Compatible Regular Expressions) সবচেয়ে সাধারণ ধরন, যা PHP, Python-এর re মডিউল এবং JavaScript সামান্য পার্থক্যসহ সমর্থন করে। POSIX grep ও sed-এ ব্যবহৃত আরও সীমিত সিনট্যাক্স সংজ্ঞায়িত করে। ভাষার মধ্যে প্যাটার্ন স্থানান্তরের সময় পার্থক্যগুলি গুরুত্বপূর্ণ: JavaScript-এ কাজ করা একটি lookahead Go-এর RE2 ইঞ্জিনে একেবারেই কম্পাইল নাও হতে পারে।
কেন অনলাইন Regex পরীক্ষক ব্যবহার করবেন?
কোড ফাইলে regex লেখার মানে প্রতিবার প্যাটার্ন পরিবর্তন করলে সেভ করা, চালানো এবং আউটপুট পরীক্ষা করা। ব্রাউজার-ভিত্তিক regex পরীক্ষক সেই ফিডব্যাক লুপ শূন্যে নিয়ে আসে: আপনি টাইপ করুন, ম্যাচ দেখুন।
Regex পরীক্ষকের ব্যবহারের ক্ষেত্র
Regex সিনট্যাক্স দ্রুত রেফারেন্স
নিচের সারণীতে সবচেয়ে বেশি ব্যবহৃত regex টোকেন দেওয়া আছে। এগুলি JavaScript, Python, Go, PHP এবং বেশিরভাগ PCRE-সামঞ্জস্যপূর্ণ ইঞ্জিনে কাজ করে। ভাষা-নির্দিষ্ট এক্সটেনশন (যেমন Python-এর conditional pattern বা JavaScript-এর \k সিনট্যাক্স সহ named group) কোড উদাহরণ বিভাগে উল্লেখ করা হয়েছে।
| প্যাটার্ন | নাম | বিবরণ |
|---|---|---|
| . | Any character | Matches any single character except newline (unless s flag is set) |
| \d | Digit | Matches [0-9] |
| \w | Word character | Matches [a-zA-Z0-9_] |
| \s | Whitespace | Matches space, tab, newline, carriage return, form feed |
| \b | Word boundary | Matches the position between a word character and a non-word character |
| ^ | Start of string/line | Matches the start of the input; with m flag, matches start of each line |
| $ | End of string/line | Matches the end of the input; with m flag, matches end of each line |
| * | Zero or more | Matches the preceding token 0 or more times (greedy) |
| + | One or more | Matches the preceding token 1 or more times (greedy) |
| ? | Optional | Matches the preceding token 0 or 1 time |
| {n,m} | Quantifier range | Matches the preceding token between n and m times |
| () | Capturing group | Groups tokens and captures the matched text for back-references |
| (?:) | Non-capturing group | Groups tokens without capturing the matched text |
| (?=) | Positive lookahead | Matches a position followed by the given pattern, without consuming it |
| (?<=) | Positive lookbehind | Matches a position preceded by the given pattern, without consuming it |
| [abc] | Character class | Matches any one of the characters inside the brackets |
| [^abc] | Negated class | Matches any character not inside the brackets |
| | | Alternation | Matches the expression before or after the pipe |
Regex ফ্ল্যাগ ব্যাখ্যা
ফ্ল্যাগ (modifier-ও বলা হয়) ইঞ্জিন কীভাবে প্যাটার্ন প্রক্রিয়া করে তা পরিবর্তন করে। JavaScript-এ শেষ স্ল্যাশের পরে যোগ করুন: /pattern/gi। Python-এ দ্বিতীয় আর্গুমেন্ট হিসেবে দিন: re.findall(pattern, text, re.IGNORECASE | re.MULTILINE)। সব ফ্ল্যাগ প্রতিটি ভাষায় পাওয়া যায় না।
| ফ্ল্যাগ | নাম | আচরণ |
|---|---|---|
| g | Global | Find all matches, not just the first one |
| i | Case-insensitive | Letters match both uppercase and lowercase |
| m | Multiline | ^ and $ match start/end of each line, not just the whole string |
| s | Dot-all | . matches newline characters as well |
| u | Unicode | Treat the pattern and subject as a Unicode string; enables \u{FFFF} syntax |
| y | Sticky | Matches only from the lastIndex position in the target string |
কোড উদাহরণ
JavaScript, Python, Go এবং কমান্ড লাইনে কার্যকর regex উদাহরণ। প্রতিটি উদাহরণে প্যাটার্ন তৈরি, ম্যাচ এক্সট্র্যাকশন এবং আউটপুট দেখানো হয়েছে।
// Match all email addresses in a string
const text = 'Contact us at support@example.com or sales@example.com'
const emailRegex = /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g
const matches = text.matchAll(emailRegex)
for (const match of matches) {
console.log(match[0], 'at index', match.index)
}
// → "support@example.com" at index 14
// → "sales@example.com" at index 37
// Named capture groups (ES2018+)
const dateRegex = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/
const result = '2026-03-30'.match(dateRegex)
console.log(result.groups)
// → { year: "2026", month: "03", day: "30" }
// Replace with a callback
'hello world'.replace(/\b\w/g, c => c.toUpperCase())
// → "Hello World"import re
# Find all IPv4 addresses
text = 'Server 192.168.1.1 responded, fallback to 10.0.0.255'
pattern = r'\b(?:\d{1,3}\.){3}\d{1,3}\b'
matches = re.findall(pattern, text)
print(matches) # → ['192.168.1.1', '10.0.0.255']
# Named groups and match objects
date_pattern = r'(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})'
m = re.search(date_pattern, 'Released on 2026-03-30')
if m:
print(m.group('year')) # → '2026'
print(m.group('month')) # → '03'
# Compile for repeated use (faster in loops)
compiled = re.compile(r'\b[A-Z][a-z]+\b')
words = compiled.findall('Hello World Foo bar')
print(words) # → ['Hello', 'World', 'Foo']package main
import (
"fmt"
"regexp"
)
func main() {
// Find all matches
re := regexp.MustCompile(`\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}\b`)
text := "Contact support@example.com or sales@example.com"
matches := re.FindAllString(text, -1)
fmt.Println(matches)
// → [support@example.com sales@example.com]
// Named capture groups
dateRe := regexp.MustCompile(`(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})`)
match := dateRe.FindStringSubmatch("2026-03-30")
for i, name := range dateRe.SubexpNames() {
if name != "" {
fmt.Printf("%s: %s\n", name, match[i])
}
}
// → year: 2026
// → month: 03
// → day: 30
// Replace with a function
result := re.ReplaceAllStringFunc(text, func(s string) string {
return "[REDACTED]"
})
fmt.Println(result)
// → Contact [REDACTED] or [REDACTED]
}# Find lines matching an IP address pattern
grep -E '\b([0-9]{1,3}\.){3}[0-9]{1,3}\b' access.log
# Extract email addresses from a file
grep -oE '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}' contacts.txt
# Replace dates from YYYY-MM-DD to DD/MM/YYYY using sed
echo "2026-03-30" | sed -E 's/([0-9]{4})-([0-9]{2})-([0-9]{2})/\3\/\2\/\1/'
# → 30/03/2026
# Count matches per file in a directory
grep -rcE 'TODO|FIXME|HACK' src/
# → src/main.js:3
# → src/utils.js:1