← Back to Bash Course | Chapter 10: Text Processing Tools | Lesson 10 of 12

Bash में Basic Regular Expressions ([[ =~ ]])

Bash directly जांच सकता है कि text का एक piece एक pattern-based rule से match करता है या नहीं, grep जैसे किसी external tool की ज़रूरत के बिना।
Syntax
bash
[[ $string =~ regex ]]
echo "${BASH_REMATCH[0]}"
echo "${BASH_REMATCH[1]}"

Basic Pattern Matching

[[ ]] के अंदर =~ operator test करता है कि left-hand string right तरफ के extended regular expression से match करती है या नहीं। [[ ]] का exit status reflect करता है कि match succeed हुआ या नहीं।

उदाहरण: Basic Pattern Matching

bash
#!/bin/bash
value="hello123"
if [[ $value =~ [0-9]+ ]]; then
    echo "contains digits"
fi

BASH_REMATCH के साथ Capturing Groups

जब एक regex में parenthesized groups शामिल हों, एक successful =~ match BASH_REMATCH array populate करता है: index 0 पूरा match है, और indices 1+ हर capture group से order में correspond करते हैं।

उदाहरण: Capturing Groups with BASH_REMATCH

bash
#!/bin/bash
version="v2.5.1"
if [[ $version =~ v([0-9]+)\.([0-9]+)\.([0-9]+) ]]; then
    echo "major: ${BASH_REMATCH[1]}"
    echo "minor: ${BASH_REMATCH[2]}"
    echo "patch: ${BASH_REMATCH[3]}"
fi

एक Pattern को Variable में Store करना

Regex को एक variable में रखना और इसे [[ ]] test में unquoted उपयोग करना accidental literal-string matching से बचता है और complex patterns को पढ़ना और reuse करना आसान बनाता है।

Warning: Pattern को quote करना (जैसे [[ $email =~ "$pattern" ]]) इसे bash में एक literal string match बना देता है, silently regex को break करते हुए।

उदाहरण: Storing a Pattern in a Variable

bash
#!/bin/bash
email="[email protected]"
pattern='^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$'
if [[ $email =~ $pattern ]]; then
    echo "looks like a valid email format"
else
    echo "does not look like an email"
fi

Input Format Validate करना

=~ से Regex matching commonly यह validate करने के लिए उपयोग होता है कि input की एक expected shape है, जैसे पूरी तरह digits होना, आगे process करने से पहले।

उदाहरण: Validating Input Format

bash
#!/bin/bash
check_number() {
    if [[ $1 =~ ^[0-9]+$ ]]; then
        echo "$1 is a valid whole number"
    else
        echo "$1 is NOT a valid whole number"
    fi
}
check_number "12345"
check_number "12a45"
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. =~ के right side पर regex को quote करना; इसे quote करना कुछ bash versions में इसे एक pattern के रूप में treat करने के बजाय एक literal string match force करता है।
  2. =~ regex syntax (POSIX extended regex) को case या filename matching में उपयोग होने वाले glob syntax से confuse करना; वे अलग metacharacters उपयोग करते हैं।
  3. यह भूल जाना कि BASH_REMATCH index 0 पर पूरा match रखता है और index 1 से capture groups, सिर्फ एक successful =~ test के ठीक बाद।
चैप्टर सारांश
  • [[ $string =~ $pattern ]] एक string को एक POSIX extended regular expression के against test करता है।
  • Pattern को unquoted छोड़ें (या इसे एक variable में store करें) ताकि bash इसे एक regex के रूप में treat करे, literal string नहीं।
  • BASH_REMATCH[0] पूरा match रखता है; BASH_REMATCH[1], [2], आदि capture groups रखते हैं।
  • =~ bash-specific है और POSIX sh या [ ] (single-bracket) tests में available नहीं है।
🔒

Chapter Quiz — Complete all 12 topics to unlock

0/12 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.