Regex Tester
किसी स्ट्रिंग के विरुद्ध रेगुलर एक्सप्रेशन का परीक्षण करें और सभी मिलान हाइलाइट देखें
पैटर्न
टेस्ट स्ट्रिंग
रेगुलर एक्सप्रेशन क्या है?
रेगुलर एक्सप्रेशन (regex या regexp) वर्णों का एक ऐसा अनुक्रम है जो किसी खोज पैटर्न को परिभाषित करता है। Regex परीक्षक आपको एक पैटर्न लिखने, उसे नमूना पाठ पर चलाने और सभी मिलान वास्तविक समय में हाइलाइट करके देखने की सुविधा देते हैं। यह अवधारणा 1950 के दशक में गणितज्ञ Stephen Kleene के नियमित भाषाओं पर किए गए कार्य से उत्पन्न हुई, और Ken Thompson ने 1968 में QED पाठ संपादक में पहला regex इंजन निर्मित किया।
एक regex इंजन किसी पैटर्न को बाएँ से दाएँ पढ़ता है और मिलान का प्रयास करते हुए इनपुट वर्णों को उपभोग करता है। जब आंशिक मिलान विफल होता है, तो इंजन बैकट्रैकिंग का उपयोग करता है: वह पीछे जाता है और पैटर्न में वैकल्पिक पथ आज़माता है। कुछ इंजन (जैसे Go में उपयोग होने वाला RE2) पैटर्न को नियतात्मक परिमित ऑटोमेटन (DFA) में रूपांतरित करके बैकट्रैकिंग से पूरी तरह बचते हैं, जो रैखिक-समय मिलान की गारंटी देता है — हालाँकि इसके बदले में back-references जैसी सुविधाएँ उपलब्ध नहीं होतीं।
Regex सिंटैक्स शिथिल रूप से मानकीकृत है। PCRE (Perl Compatible Regular Expressions) सबसे सामान्य प्रकार है, जो PHP, Python के re मॉड्यूल, और JavaScript में कुछ मामूली अंतरों के साथ समर्थित है। POSIX एक अधिक सीमित सिंटैक्स परिभाषित करता है जो grep और sed द्वारा उपयोग किया जाता है। भाषाओं के बीच पैटर्न स्थानांतरित करते समय ये अंतर महत्त्वपूर्ण हैं: JavaScript में कार्य करने वाला lookahead Go के RE2 इंजन में बिल्कुल भी संकलित नहीं हो सकता।
ऑनलाइन Regex परीक्षक क्यों उपयोग करें?
किसी कोड फ़ाइल में regex लिखने का अर्थ है — प्रत्येक बार पैटर्न समायोजित करने पर फ़ाइल सहेजना, चलाना, और आउटपुट जाँचना। ब्राउज़र-आधारित regex परीक्षक यह feedback loop खत्म कर देता है: आप टाइप करते हैं, मिलान तुरंत दिखते हैं।
Regex Tester के उपयोग के अवसर
Regex सिंटैक्स त्वरित संदर्भ
नीचे दी गई तालिका सबसे अधिक उपयोग किए जाने वाले regex टोकन को शामिल करती है। ये JavaScript, Python, Go, PHP, और अधिकांश PCRE-संगत इंजनों में कार्य करते हैं। भाषा-विशिष्ट विस्तार (जैसे Python के सशर्त पैटर्न या JavaScript के \k सिंटैक्स वाले नामांकित समूह) कोड उदाहरण खंड में उल्लिखित हैं।
| पैटर्न | नाम | विवरण |
|---|---|---|
| . | 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 फ्लैग की व्याख्या
फ्लैग (जिन्हें मॉडिफायर भी कहा जाता है) बदलते हैं कि इंजन पैटर्न को कैसे संसाधित करता है। 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