DOC PREVIEW
FSU COP 4342 - Lecture Notes

This preview shows page 1-2-23-24 out of 24 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Regular expressions and case insensitivityAs previously mentioned, you can make matching case insensitivewith the i flag:/\b[Uu][Nn][Ii][Xx]\b/; # explicitly giving case folding/\bunix\b/i; # using ‘‘i’’ flag to fold codeUnix Tools: Perl 5Really matching any character with “.”As mentioned before, usually the “.” (dot, period, full stop) matchesany character except newline. You make it match newline with the sflag:/"(.|\n)*"/; # match any quoted string, even with newlines embedded/"(.*)"/s; # same meaning, using ‘‘s’’ flagN.B. – I like to use the flags ///six; as a personal default set offlags with Perl regular expressions.Unix Tools: Perl 5Going global with the “g” flagYou can make your matching global with the g flag. For ordinarymatches, this means making them stateful: Perl will remember whereyou left off with each reinvocation of the match unless you change thevalue of the variable, which will reset the match.Unix Tools: Perl 5Going global with the “g” flag#!/usr/bin/perl -w# 2006 09 29 - rdl Script36.pl# shows the //g as stateful...while(<>){while(/[A-Z]{2,}/g){print "$&\n" if (defined($&));}}Unix Tools: Perl 5Interpolating variables in patternsYou can even specify a variable inside of a pattern – but you want tomake sure that it gives a legitimate regular expression.Unix Tools: Perl 5Interpolating variables in patternsmy $var1 = "[A-Z]*";if( "AB" =~ /$var1/ ){print "$&";}else{print "nopers";}# yieldsABUnix Tools: Perl 5Regular expressions and substitutionThe s/.../.../ form can be used to make substitutions in thespecified string.If paired delimiters are used, then you have to use two pairs ofthe delimiters.g after the last delimiter indicates to replace more than just thefirst occurrence.The substitution can be bound to a string. Otherwise it makes thesubstitutions in $_.The operation returns the number of replacements performed,which can be more than one with the ’g’ option.Unix Tools: Perl 5Examples#!/usr/bin/perl -w# 2006 09 29 - rdl Script37.pl# shows s///g... by removing acronymsuse strict;while(<>){s/([A-Z]{2,})//g;print;}Unix Tools: Perl 5Exampless/\bfigure (\d+)/Figure $1/ # capitalize references to figuress{//(.*)}{/\*$1\*/} # use old style C commentss!\bif(!if (! # put a blanks(!)(.) # tone down that messages[!][.]g # replace all occurrences of ’!’ with ’.’Unix Tools: Perl 5Case shiftingYou can use \U and \L to change follows them to upper and lowercase:Unix Tools: Perl 5Case shifting$text = " the acm and the ieee are the best! ";$text =~ s/acm|ieee/\U$&/g;print "$text\n";# yieldsthe ACM and the IEEE are the best!Unix Tools: Perl 5Case shifting$text = "CDA 1001 and COP 3101are good classes, but COP 4342 is better!";$text =~ s/\b(COP|CDA) \d+/\L$&/g;print "$text\n";# yieldscda 1001 and cop 3101are good classes, but cop 4342 is better!Unix Tools: Perl 5Using tr/// (also known as y///)In Perl you can also convert one set of characters to another usingthe tr/.../.../ form. (Or if you like, you can use y///.)Much like the program tr, you specify two lists of characters,the first to be substituted, and the second what to substitute.tr returns the number of items substituted (or deleted.)The modifer d deletes characters not replaced.The modifer s “squashes” any repeated characters.Unix Tools: Perl 5Examples (from the perlop man page)$ARGV[1] =~ tr/A-Z/a-z/; # canonicalize to lower case$cnt = tr/*/*/; # count the stars in $_$cnt = $sky =~ tr/*/*/; # count the stars in $sky$cnt = tr/0-9//; # count the digits in $_Unix Tools: Perl 5More examples# get rid of redundant blanks in $_tr/ //s;# replace [ and { with ( in $text$text =~ tr/[{/(/;Unix Tools: Perl 5Using splitThe split function breaks up a string according to a specifiedseparator pattern and generates a list of the substrings.Unix Tools: Perl 5Using substringFor example:$line = " This sentence contains five words. ";@fields = split / /, $line;map { print "$count --> $fields[$count]\n"; $count++; } @fields;# yields-->1 --> This2 --> sentence3 --> contains4 --> five5 --> words.Unix Tools: Perl 5Using the join functionThe join function does the reverse of the split function: it takes alist and converts to a string.However, it is different in that it doesn’t take a pattern as its firstargument, it just takes a string:@fields = qw/ apples pears cantaloupes cherries /;$line = join "<-->", @fields;print "$line\n";# yieldsapples<-->pears<-->cantaloupes<-->cherriesUnix Tools: Perl 5Filehandles[Also see man perlfaq5 for more detail on this subject.]A filehandle is an I/O connection between your process and somedevice or file. Perl output is buffered.Perl has three predefined filehandles: STDIN, STDOUT, andSTDERR.Unix Tools: Perl 5FilehandlesUnlike other variables, you don’t declare filehandles. The conventionis to use all uppercase letters for filehandle names. (Especiallyimportant if you deal with anonymous filehandles!)The open operator takes two arguments, a filehandle name and aconnection (e.g. filename). The connection can start with "" indicateread, write, and append access.Unix Tools: Perl 5Closing filehandlesThe close operator closes a filehandle. This causes any remainingoutput data associated with this filehandle to be flushed to the file.Perl automatically closes filehandles at the end of a process, or if youreopen it.Unix Tools: Perl 5Examplesclose IN; # closes the IN filehandleclose OUT; # closes the OUT filehandleclose LOG; # closes the LOG filehandleUnix Tools: Perl 5Testing openYou can check the status of opening a file by examining the result ofthe open operation. It returns a true value if it succeeded, and a falseone if it failed.Unix Tools: Perl 5Reopening a filehandleYou can reopen a standard filename. This allows you to perform inputor output in a normal fashion, but to redirect the I/O from/to a filewithin the Perl program.Unix Tools: Perl


View Full Document

FSU COP 4342 - Lecture Notes

Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?