Home Perl Regex

Operators

  • m
  • s
  • tr
  • qr
  • split
  • m operator

    $str    = "123abcd456";
    @match  = $str =~ /\d/g;  # 1 2 3 4 5 6
    $str = "yes is your answer."; $yes = "yes";
    print "match\n" if ( $str =~ m/$yes/ );  # match
    print "match\n" if ( $str =~ m'$yes' );  #
    open (DICT,"/usr/share/dict/words") or die "Can't open dictionary: $!";
    while ( <DICT> ) {
    	  $first_word = $1 if ?(^aba.*)?;
    	  $last_word  = $1 if /(^aba.*)/;
    }
    print "$first_word\n";  # aback
    print "$last_word\n";   # abating
    $text = "3 2 1 wow!";
    # use /gc to remember position
    while ( $text =~ /(\d)/gc ){ print "$1...\n"; }
    # use \G to match  rest of text
    if ( $text =~ /\G\s*(.+)$/ ) { print $1, "!\n"; }
    #  3...
    #  2...
    #  1...
    #  wow!
    $text = "proton";
    while ( $text =~ /(..)/g ) { print "[$1]"; }
    # [pr][ot][on]
    while ( $text =~ /(?=(..))/g ) { print "[$1]"; }
    # [pr][ro][ot][to][on]
    $string = "one=1 two=2 three=3";
    %hash = $string =~ m/(\w+)=(\w+)/g;

    s operator

    $pop = "12398712376";
    $pop =~ s/(?<=\d)(?=(?:\d{3})+$)/,/;
    print "$pop\n";
    # 12,398712376
    $pop = "12398712376";
    $pop =~ s/(?<=\d)(?=(?:\d{3})+$)/,/g;
    print "$pop\n";
    # 12,398,712,376
    $pop = "12398712376";
    $pop =~ s/(\d)(\d{3})/$1,$2/g;
    print "$pop\n";
    # 1,2398,712376
    $pop = "12398712376 345345345";
    while ( $pop =~ s/(\d)(\d{3})\b/$1,$2/ ){ print "$pop\n"; }
    # 12398712,376 345345345
    # 12398,712,376 345345345
    # 12,398,712,376 345345345
    # 12,398,712,376 345345,345
    # 12,398,712,376 345,345,345
    $pop = "12398712376 345345345";
    while ( $pop =~ s/(\d)(\d{3})\b/$1,$2/g ){ print "$pop\n"; }
    # 12398712,376 345345,345
    # 12398,712,376 345,345,345
    # 12,398,712,376 345,345,345
    $a = 123;
    $a =~ s/2/2*4/e;
    # 183
    
    $b = 123; $c = 2*2;
    $b =~ s/2/" $c*2 "/e;
    # 1 4*2 3
    
    $b = 123; $c = 2*2;
    $b =~ s/2/" $c*2 "/ee;
    # 183
    $string = "anatoliy";
    $string =~ s/(\w)/uc($1)/ge;
    # ANATOLIY
    $str = "1.Smith\n\tJey\n\tAn\n2.Shevchenko\n\tTaras\n";
    while ( $str =~ /(^\d.*$)/gm ) {
    	print "$1\n";
    }
    # 1.Smith
    # 2.Shevchenko
    $_ = "1\n2\n9";
    $_ =~ s/./+/g;
    # +
    # +
    # +
    $_ =~ s/./+/sg;
    # +++++
    $str = "aBcDe";
    $num = $str =~ s/([a-z])/uc($1)/eg;
    print "\$str: $str\n"; # $str: ABCDE
    print "\$num: $num\n"; # $num: 3
    
    $str = "aBcDe";
    ($num = $str) =~ s/([a-z])/uc($1)/eg;
    print "\$str: $str\n"; # $str: aBcDe
    print "\$num: $num\n"; # $num: ABCDE
    # put commas in the right places an integer ( from "Camel Book" ).
    $_ = "2343454567";
    1 while s/(\d)(\d{3})(?!\d)/$1,$2/;
    print "$_\n"; # 2,343,454,567
    # expand tabs to 8-column spacing ( from "Camel Book" ).
    $_ = "abc\t\t\tABC\t3";
    1 while s/\t+/'.' x (length($&)*8 - length($`)%8)/e;
    print "$_\n";
    # abc.....................ABC.....3
    # remove nested remarks ( from "Camel Book" ).
    $_ = "abc (123(second) ( third )) the end";
    1 while s/\([^()]*\)//g;
    print "$_\n"; # abc  the end

    tr operator

    $str = "Hello World!";
    $str =~ tr/a-z/./; # H.... W....!
    
    $str = "Hello World!";
    $str =~ tr/a-z//d; # H W!
    
    $str = "Hello World!";
    $str =~ tr/a-z//cd; # elloorld
    
    $str = "Hello World!";
    $str =~ tr/a-z/./c; # .ello..orld.
    
    $str = "Heellooo WWoorld!!";
    $str =~ tr/a-z//s; # Helo WWorld!!

    qr operator

    my $np;
    $np = qr{\((?:(?>[^()]+)|(??{$np}))*\)}x;
    my $funpat = qr/\w+$np/;
    my $s = 'my_function_call(1,(2*(3+4)),5)';
    if ( $s =~ /^$funpat$/ ){ print "match\n"; }
    #  match
    
    $palindrom = qr/^ (.+) .? (??{quotemeta reverse $1}) $/xi;
    print "Pattern: $palindrom\n";
    for ( qw(abcba a bddb abvcba), "" ){
    	print "",
    	(($_ !~ /$palindrom/) ? "NOT " : "") . "MATCH => $_\n";
    }
    #  Pattern: (?ix-sm:^ (.+) .? (??{quotemeta reverse $1}) $)
    #  MATCH => abcba
    #  NOT MATCH => a
    #  MATCH => bddb
    #  NOT MATCH => abvcba
    #  NOT MATCH =>
    
    # match nested parentheses
    my $p;
    $p = qr{(?: ({) | (<)) (?:(?>(?(1)[^{}]+|(?(2)[^<>]+)))|
    			(??{$p}))*
    			( ?(1)} | (?(2)>) )
    		 }x;
    my $mp = qr/m$p/;
    my $s = "test m<one<two>> the end";
    if ( $s =~ /$mp/ ){ $s =~ s/$mp/MATCH/; print "$s\n";}
    #  test MATCH the end

    split operator

    # "Mastering Regular Expressions", 2nd edition, ch7.
    
    my $text = "IO.SYS:22558:95-10-03:s-sh:optinal";
    print qq('$_', ) for ( split(/:/=>$text) );
    # 'IO.SYS', '22558', '95-10-03', 's-sh', 'optinal',
    print qq('$_\', ) for ( split(/:/ ,$text, 3) );
    # 'IO.SYS', '22558', '95-10-03:s-sh:optinal', 
    my ($x,$y,$z) = split (/:/=>$text);
    print "( \$x=$x, \$y=$y, \$z=$z )\n";
    # ( $x=IO.SYS, $y=22558, $z=95-10-03 )
    
    my @Paragraphs = split(m/\s*<p>\s*/i => $html); # by paragraphs
    my @Lines      = split(m/^/m => $lines);        # logic lines
    
    print qq('$_', ) for ( split(//   => "  a short  test  ") );
    # ' ', ' ', 'a', ' ', 's', 'h', 'o', 'r', 't', ' ', ' ', 't', 'e', 's', 't', ' ', ' ', 
    print qq('$_', ) for ( split(' '  => "  a short  test  ") );
    # 'a', 'short', 'test', 
    print qq('$_', ) for ( split(/\s+/=> "  a short  test  ") ); # to save beginning
    # '', 'a', 'short', 'test', 
    print qq('$_', ) for ( split(/ /  => "  a short  test  ",-1) ); # !!
    # '', '', 'a', 'short', '', 'test', '', '', 
    print qq('$_', ) for ( split(/s+/ => "  a short  test  ") ); # !!
    # '  a ', 'hort  te', 't  ', 
    
    print qq('$_', ) for ( split(/(\s)+/=> "  a short  test  ") );
    # '', ' ', 'a', ' ', 'short', ' ', 'test', ' ', 
    print qq('$_', ) for ( split(/(\s+)/=> "  a short  test  ") );
    # '', '  ', 'a', ' ', 'short', '  ', 'test', '  ', 
    print qq('$_', ) for ( split(/( )/  => "  a short  test  ") );
    # '', ' ', '', ' ', 'a', ' ', 'short', ' ', '', ' ', 'test', ' ', '', ' ',