Modifiers का उपयोग
Closing slash के बाद Letters यह बदलते हैं कि एक pattern कैसे behave करता है।
In this page:
Modifiers
i modifier case ignore करता है, g बार-बार match करता है, m ^ और $ को हर line पर match होने देता है, s dot को एक newline से match होने देता है, और x pattern के अंदर whitespace और comments allow करता है। Modifiers combine हो सकते हैं, जैसे /gi। x modifier complicated patterns को readable रखने का एक अच्छा तरीका है।
Note:
/x के अंतर्गत, whitespace ignore होता है, इसलिए जब आपको एक literal space चाहिए \s या [ ] लिखें।
उदाहरण: Modifiers
use strict;
use warnings;
print "i: ", ("PERL" =~ /perl/i ? "match" : "no match"), "\n";
my $text = "line one\nline two\nline three";
my @starts = $text =~ /^(\w+)/mg;
print "m with g: @starts\n";
print "without s: ", ("a\nb" =~ /a.b/ ? "match" : "no match"), "\n";
print "with s: ", ("a\nb" =~ /a.b/s ? "match" : "no match"), "\n";
my $re = qr/
(\d{3}) # area code
-
(\d{4}) # number
/x;
if ("555-1234" =~ $re) {
print "area=$1 number=$2\n";
}
# Output:
# i: match
# m with g: line line line
# without s: no match
# with s: match
# area=555 number=1234
Login to try C/C++/Java/PHP code in the editor
आम गलतियां
- /i भूल जाना और uppercase matches miss करना
- एक multi-line string की हर line anchor करते वक्त /m भूल जाना
- /x pattern में spaces छोड़ना और उम्मीद करना कि वे match करेंगे
चैप्टर सारांश
- /i case ignore करता है
- /m ^ और $ को per line match करने देता है
- /s . को एक newline से match होने देता है
- /x whitespace और comments allow करता है
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: