Matching की Basics
=~ operator test करता है कि एक string में एक pattern है या नहीं।
In this page:
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
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
Login to try C/C++/Java/PHP code in the editor
आम गलतियां
- =~ की बजाय = लिखना और match करने की बजाय assign करना
- यह भूल जाना कि जब तक anchored न हो एक pattern string में कहीं भी match करता है
- एक 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: