← Back to Perl Course | Chapter 4: Regular Expressions | Lesson 1 of 7

Matching की Basics

=~ operator test करता है कि एक string में एक pattern है या नहीं।

In this page:

  1. Matching Basics

Matching Basics

एक pattern slashes के बीच लिखा जाता है, जैसे /perl/, और binding operator =~ इसे एक string पर apply करता है। Match true या false return करता है, इसलिए यह एक if statement में naturally fit होता है, और !~ test negate करता है। =~ के बिना, एक pattern default variable $_ के against match होता है।

Note: जब pattern में कई slashes हों एक अलग delimiter जैसे m{...} इस्तेमाल करें।

उदाहरण: Matching basics

perl
use strict;
use warnings;

my $text = "The quick brown fox";
if ($text =~ /quick/) {
    print "found 'quick'\n";
}
print "no 'slow'\n" if $text !~ /slow/;
print "starts with The\n" if $text =~ /^The/;
print "ends with fox\n" if $text =~ /fox$/;
for (qw(cat cart dog)) {
    print "$_ matches ca\n" if /ca/;
}
my $path = "/usr/local/bin";
print "path matches\n" if $path =~ m{^/usr/local};

# Output:
# found 'quick'
# no 'slow'
# starts with The
# ends with fox
# cat matches ca
# cart matches ca
# path matches
{# 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. =~ की बजाय = लिखना और match करने की बजाय assign करना
  2. यह भूल जाना कि जब तक anchored न हो एक pattern string में कहीं भी match करता है
  3. एक pattern test करने के लिए == या eq इस्तेमाल करना
चैप्टर सारांश
  • /pattern/ =~ string एक match test करता है
  • !~ no match test करता है
  • एक match string में कहीं भी succeed होता है
  • $_ default target है
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 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.