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

Modifiers का उपयोग

Closing slash के बाद Letters यह बदलते हैं कि एक pattern कैसे behave करता है।

In this page:

  1. Modifiers

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

perl
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
{# 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. /i भूल जाना और uppercase matches miss करना
  2. एक multi-line string की हर line anchor करते वक्त /m भूल जाना
  3. /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:

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.