1 / 29

Regular Expressions

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.

iago
Download Presentation

Regular Expressions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Regular Expressions

  2. 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..

  3. 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”;

  4. 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

  5. Other Delimeters • Like m// and qw//, can use different delimeters: s#^https://#http://# s{^https://}(http://); s%wilma%Wilma#gi

  6. Binding Operator • Same as for matches: $url =~ s#^https://#http://#

  7. 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

  8. 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.

  9. 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

  10. 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;

  11. Split - Defaults $_ = “This is a\t test”; My @fields = split /\s+/; my @fields = split; print “@fields” # gives “This is a test”

  12. 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

  13. 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+)/;

  14. More m// in list context My $text = “2 quick brown foxes”; My @words = ($text =~/([a-z]+)/ig); @words elements: quick brown foxes

  15. 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

  16. 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

  17. 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) {

  18. File Tests

  19. 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!"; }

  20. 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; }

  21. 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

  22. 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” }

  23. 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

  24. 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

  25. localtime • - In a list context, localtime returns lots of numbers: my ($sec, $min, $hour, $day, $mon, $year, $wday, $yday, $isdst) = localtime $timestamp;

  26. 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)

  27. 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".

  28. 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 _

  29. 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, $_ } }

More Related