Why the String We Want to Match Upon

Embed Size (px)

Citation preview

  • 8/9/2019 Why the String We Want to Match Upon

    1/15

    Regular Expressions

    ############################################################################### name: main# purpose: show regular expression usage##############################################################################

    # matching$call911 = 'Someone, call 911.'; # the string we want to match upon$found = ($call911 =~ /call/); # $found is TRUE, matched 'call'@res = ($call911 =~ /Some(...)/); # @res is ('one'), matched 'Someone'$entire_res = $&; # $entire_res is 'Someone'$brack1_res = $1; # $brack1_res is 'one', $+ for last brackets($entire_pos,$brack1_pos) = @-; # $entire_pos is 0, $brack1_pos is 4($entire_end,$brack1_end) = @+; # $entire_end is 7, $brack1_end is 7# global matching (get all found)$call911 =~ /(.o.)/g; # g is global-match, $1 is 'Som', $2 is 'eon'

    @res = ($call911 =~ /(.o.)/g); # @res is ('Som','eon'), $& is 'eon'

    # substituting$greeting = "hello world"; # the string we want to replace in$greeting =~ s/hello/goodbye/; # $greeting is 'goodbye world'

    # splitting@l = split(/\W+/,$call911); # @l is ('Someone','call','911')@l = split(/(\W+)/,$call911); # @l is ('Someone',', ','call',' ','911','.')

    # pattern syntax$call911 =~ /c.ll/; # . is anything but \n, $& is 'call'$call911 =~ /c.ll/s; # s is singe-line, . will include \n, $& is 'call'$call911 =~ /911\./; # \ escapes metachars {}[]()^$.|*+?\, $& is '911.'$call911 =~ /o../; # matches earliest, $& is 'ome'$call911 =~ /g?one/; # ? is 0 or 1 times, $& is 'one'$call911 =~ /cal+/; # + is 1 or more times, $& is 'call', * for 0 or more$call911 =~ /cal{2}/; # {2} is exactly 2 times, $& is 'call'$call911 =~ /cal{0,3}/; # {0,3} is 0 to 3 times, $& is 'call', {2,} for >= 2$call911 =~ /S.*o/; # matches are greedy, $& is 'Someo'$call911 =~ /S.*?o/; # ? makes match non-greedy, $& is 'So'

    $call911 =~ /^.o/; # ^ must match beginning of line, $& is 'So'$call911 =~ /....$/; # $ must match end of line, $& is '911.'$call911 =~ /9[012-9a-z]/;# one of the letters in [...], $& is '91'$call911 =~ /.o[^m]/; # none of the letters in [^...], $& is 'eon'$call911 =~ /\d*/; # \d is digit, $& is '911'$call911 =~ /S\w*/; # \w is word [a-zA-Z0-9_], $& is 'Someone'$call911 =~ /..e\b/; # \b is word boundry, $& is 'one', \B for non-boundry$call911 =~ / \D.../; # \D is non-digit, $& is ' call', \W for non-word$call911 =~ /\s.*\s/; # \s is whitespace char [\t\n ], $& is ' call '$call911 =~ /\x39\x31+/; # \x is hex byte, $& is '911'$call911 =~ /Some(.*),/; # (...) extracts, $1 is 'one', $& is 'Someone,'

    $call911 =~ /e(one|two)/; # | means or, $& is 'eone'$call911 =~ /e(?:one|tw)/;# (?:...) does not extract, $& is 'eone', $1 is undef$call911 =~ /(.)..\1/; # \1 is memory of first brackets, $& is 'omeo'

  • 8/9/2019 Why the String We Want to Match Upon

    2/15

    $call911 =~ /some/i; # i is case-insensitive, $& is 'Some'$call911 =~ /^Some/m; # m is multi-line, ^ will match start of entire text$call911 =~ m!call!; # use ! instead of /, no need for \/, $& is 'call'

    Special Variables

    ############################################################################### name: main# purpose: show some special internal variables##############################################################################

    # $_ - default input print for (1..10); # in many places, no var will cause work on $_print $_ for $_ (1..10); # same as above

    # $. - current line in last file handlewhile (!( =~ /error/i)) {};

    print "first error on line $.\n";

    # $/ - input record separator (default is "\n")undef $/;$entire = ; # read entire file all at once$/ = "512";$chunk = ; # read a chunk of 512 bytes

    # $\ - output record separator (default is undef)$\ = "\n"; # auto \n after print

    print 'no need for LF';

    # $! - errno / a string description of erroropen(FILE) or die "error: $!";

    # $@ - errors from last evaleval $cmd;

    print "eval successful" if not $@;

    Standard IO

    ############################################################################### name: main# purpose: show some basic IO and file handling##############################################################################

    # open a file a la shellopen(IN, "< input.txt") or die "cant open input file: $!";open(OUT, ">> output.txt") or die "cant open output file: $!";# binmode(IN) to change IN from txt mode to binary mode

    # read records from a file (according to $/)

    while ($line = ) # returns next line, or FALSE if none left{

    # write data to a file

  • 8/9/2019 Why the String We Want to Match Upon

    3/15

    print OUT $line;}

    # cleanupclose(IN);close(OUT);

    # check if file existsprint "$filename exists" if (-e $filename);

    # check the file sizeprint "$filename file size is ".(stat $filename)[7];

    # get all the txt files in current directory@txtfiles = ; # perl globbing@txtfiles = `dir /b *.txt`; # or use the shell (slower), needs chomping

    Useful Functions and Keywords

    ############################################################################### name: main# purpose: show some basic functions and keywords of perl##############################################################################

    # scalar / string functionsforeach (`dir /b`) { chomp; print; } # chomp removes \n tail (according to $/)$ext = chop($file).$ext for (1..3); # chop removes last char and returns it

    print 'a is '.chr(ord('a')); # ord converts chr to num, chr is opposite print lc("Hello"), uc(" World"); # prints 'hello WORLD' print length("hello"); # prints '5'$three_a = sprintf("%08x",58); # just like regular c sprintf

    print($type) if ($type = ref $ref); # prints 'SCALAR'/'ARRAY'/'HASH'/'REF'

    # regexps and pattern matching functions print quotemeta('[.]'); # prints '\[\.\]'@words = split(/W+/,$sentence); # splits a string according to a regexp

    # array / list functions

    @three_two_one = reverse(1,2,3); # returns a list in reverse print pop(push(@arr,'at end')); # prints 'at end', no change to @arrprint shift(unshift(@arr,'at start'); # prints 'at start', no change to @arr@after = grep(!/^\s*#/, @before); # weed out full comment lines$sentence = join(' ',@words); # turns lists into strings with a delim

    print sort ; # sort string lists in alphabetical orderdelete @arr[3..5]; # deletes the 3rd,4th,5th elements in @arr

    print "length is ".scalar @arr; # scalar evaluates expressions as scalars

    # hash related functionsdelete @hash{"key1","key2"}; # deletes these keys from the hash

    print $hash{$_} foreach (keys %hash); # prints all hash values by checking keys print values(%hash); # same but different

  • 8/9/2019 Why the String We Want to Match Upon

    4/15

    # misc functions and keywordssleep(10); # causes the script to sleep for 10 secsexit(0) if $should_quit; # exits the script with a return valueuse warnings; use strict; # imports new external modulesno warnings; no strict; # un-imports imported external modulesmy $var; # declare a local variable (strict)

    undef($null) if defined($null); # check if a variable is definedeval '$pn = $0;'; print $pn; # interpret new perl code in runtimesystem("del $filename"); # run commands in the shell (blocking)system("start calc.exe"); # run commands in the shell (nonblocking)@files = `dir /b`; # run & get output of shell commands ("")

  • 8/9/2019 Why the String We Want to Match Upon

    5/15

  • 8/9/2019 Why the String We Want to Match Upon

    6/15

    arya

    married

    with

    manohara

  • 8/9/2019 Why the String We Want to Match Upon

    7/15

  • 8/9/2019 Why the String We Want to Match Upon

    8/15

    Anngi pu

  • 8/9/2019 Why the String We Want to Match Upon

    9/15

  • 8/9/2019 Why the String We Want to Match Upon

    10/15

    Anngi pu

  • 8/9/2019 Why the String We Want to Match Upon

    11/15

    Anggi

    Anngi pu

  • 8/9/2019 Why the String We Want to Match Upon

    12/15

  • 8/9/2019 Why the String We Want to Match Upon

    13/15

  • 8/9/2019 Why the String We Want to Match Upon

    14/15

    Anngi pu

  • 8/9/2019 Why the String We Want to Match Upon

    15/15