290 likes | 389 Views
Regular Expressions. Substitutions. $_ = “green scaly dinosaur”; s/(w+) (w+)/$2, $1/; #scaly, green dino.. s/^/huge, /; # huge, scaly, green dino.. s/,.*een// # huge dinosaur s/green/red/; # no match! s/w+$/($`!)$&/; # huge (huge !)dinosaur s/s+(!W+)/$1 /; # huge (huge!) dino.
E N D
Substitutions $_ = “green scaly dinosaur”; s/(\w+) (\w+)/$2, $1/; #scaly, green dino.. s/^/huge, /; # huge, scaly, green dino.. s/,.*een// # huge dinosaur s/green/red/; # no match! s/\w+$/($`!)$&/; # huge (huge !)dinosaur s/\s+(!\W+)/$1 /; # huge (huge!) dino..
Global Replacements • s/// will only make one replacement per line. • use the g to make all non-overlapping replacements $_ = “home sweet home”; s/home/cave/g; print “$_\n”;
Option Modifiers • All the pattern match modifiers are allowed • g global • i case insensitive • s allow \n to be matched by “.” • x whitespace in regex for readability
Other Delimeters • Like m// and qw//, can use different delimeters: s#^https://#http://# s{^https://}(http://); s%wilma%Wilma#gi
Binding Operator • Same as for matches: $url =~ s#^https://#http://#
Case Shifting \U Upper case until told to stop \L Lower case until told to stop \u Upper case next letter \l Lower case next letter \E Turn off case shifting
Case Shifting Cont… $_ = “I saw Barney with Fred”; s/(fred|barney)/\U$1/gi; # I saw BARNEY with FRED s/(fred|barney)/\L$1/gi; # I saw barney with fred s/(\w+) with (\w+)/\U$2\E with $1/i; # I saw FRED with barney You can also use this technique in a double quoted string.
The Split Operator • Splits data with a common delimeter @fields = split /separator/, $string $fruit = “apple, pear, orange”; @fruits = split /,\s*/, $fruit print “@fruits” #apple pear orange • $fruits = apple,,pear,orange
Split - Empty Fields $fruits = apple,,pear,orange • By default, all trailing empty fields are ignored: @fields = split /:/, “:::a:b:c:::”; @fields now contains (‘’,’’,’’,’a’,’b’,’c’); - To get all fields, pass -1 to split: @fields = split /:/, “:::a:b:c:::”, -1;
Split - Defaults $_ = “This is a\t test”; My @fields = split /\s+/; my @fields = split; print “@fields” # gives “This is a test”
Join • The opposite of split. my $result = join $glue, @pieces; eg my $x = join “:”, 4, 6, 8, 10, 12 Gives: 4:6:8:10:12
m// in list context • If you perform a match in list context, list is populated with the memory vars: $_ = “Hello there, neighbor!”; my ($a, $b, $c) = /(\S+) (\S+) (\S+)/;
More m// in list context My $text = “2 quick brown foxes”; My @words = ($text =~/([a-z]+)/ig); @words elements: quick brown foxes
Non Greedy Quantifiers • The quantifiers we have seen so far are greedy - they will match as much as possible. • Add a ? to your quantifier to make it non greedy
Non Greedy Quantifiers $string = “I want to <b>remove</b> the <b>bold</b> tags”; Greedy: s#<b>(.*)</b>#$1#g Non-Greedy s#<b>(.*?)</b>#$1#g
Matching Multiline Text • If your string has newlines, use the m modifier and ^ and $ will now match the beginning and end of each line in the string $_ = “I’m a little teapot\nShort and Stout\n” if (/^Short\b/im) {
File Tests • Can check to see if files exist, how old they are, size, and much more. if -e $filename { print "That file exists!"; } if -M CONFIG > 28 { print “Not updated for more than a month!"; }
File Tests my @files = qw/ fred barney betty wilma /; my @big_old_files; foreach my $file (@files) { if -s $file > 100_000 and -A $file > 90; { push @big_old_files, $file; }
File Tests • -r, -w, -x and -o are for testing permissions and ownership. • -s returns length in bytes • -f, -d, -l, -S, -p, -b, and -c tests for 7 types of unix files. • -M, -A and -C no. of days since last modified, accessed or inode change
File Tests • -T and -B try to determine if a file is text or binary • -t returns true if given filehandle is a TTY if (-t STDIN) { print “Interactive mode” }
stat and lstat • Stat returns all the information available about a file. my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat ($filename); • Use lstat for information about a symbolic link
localtime • Dates from stat are secs since EPOCH • Convert to a nice string with localtime in a scalar context: my $timestamp = 1080630098 my $date = localtime $timestamp; print $date; # Tue Mar 30 15:01:38 2004
localtime • - In a list context, localtime returns lots of numbers: my ($sec, $min, $hour, $day, $mon, $year, $wday, $yday, $isdst) = localtime $timestamp;
Bitwise Operators • Sometimes you need to deal with numbers one bit at a time: 0 & 12 Bitwise And 10 | 12 Bitwise or 10 ^ 12 Bitwise xor 66 << 2 Bitwise shift to the left. 25 >> 2 Bitwise shift right. ~ 10 Bitwise negation (bit complement)
Using Bitstrings • All of the bitwise operators will work with bitstrings as well as integers. • Integer operands give integer results • If the operand is a string, perl will assume it is a bitstring: ”\xAA" | ”\x55" will give the string ”\xFF".
Underscore File Handle • Every file test asks the system for a stat buffer • System calls are slow, so to avoid looking up the same file info twice, use _
Underscore File Handle Example my @files = qw / fred barney betty wilma/; my @big_old_files; foreach (@files) { if ((-s) > 100_000 and -A _ > 90) { push @big_old_files, $_ } }