1 / 31

Perl Regular Expressions

Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression. Pattern matching Find out if a string contains some specific pattern. Substitution Replace parts of a string with other strings. Translation Replace one character with another character. Split function

josephinec
Download Presentation

Perl 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. Perl Regular Expressions

  2. Things Perl Can Do Easily with Regular Expression • Pattern matching • Find out if a string contains some specific pattern. • Substitution • Replace parts of a string with other strings. • Translation • Replace one character with another character. • Split function • Split a string into substrings

  3. Pattern Matching • if ($string =~ m/pattern/) • The result is TRUE ($string contains substring pattern) • The result is false ($string does not contains substring pattern) $message=“I like C, C++, Perl, Java, and Python.” if ($message =~ m/Perl/) { print “I see Perl\n”; } else { print “I did not see Perl\n”; };

  4. Pattern Matching to Verify Email @mail = ('peter@verhas.com‘,'hab&cnn.com’); foreach $address (@mail) { if($address =~ m/@/) { print “$address seems to be a good eMail address\n"; } else { print "$address is a bad address\n"; } } OUTPUT: peter@verhas.com seems to be a good eMail address hab&cnn.com is a bad address

  5. Substitution • String substition • In string $string, replace every "Bill Clinton" with an “Bush”: $string =~ s/Bill Clinton/Bush/;

  6. Translation • Replace one character with another character • In $string, make all vowels upper case: $string =~ tr/[a,e,i,o,u,y]/[A,E,I,O,U,Y]/; • Change every letter in $string to upper case: $string =~ tr/[a-z]/[A-Z]/; • Change every letter in $string to lower case $string =~ tr/[A-Z]/[a-z]/;

  7. Split function • Split a string into substrings • Split on character: $data = 'Becky Lincoln,25,female,South Bend'; @values = split(/,/, $data); • Split on string: $data = ‘Care Bears~~~10:30am~~~Saturday~~~CBS'; @values = split(/~~~/, $data);

  8. Wildcard Character and Repetition • Wildcard character \w  Match "word" character (alphanumeric plus "_") \W  Match non-word character \s  Match whitespace character (tab or space key) \d  Match digit character \t  Match tab \n  Match newline • Repitition +      Match 1 or more times * Match 0 or more times {n}   Match exactly n times

  9. Task 1 • Write a Perl program that can • Convert the following string to an array of word • Convert upper case letters into lower case • Display each word in one line “Hello There Nice to SEE Everyone”

  10. Perl Program for Task 1 #!/usr/bin/perl $data="Hello There Nice to SEE Everyone"; $data =~ tr/[A-Z]/[a-z]/; @words=split(/\s/, $data); # @words=split(/ /, $data); foreach $word (@words) { print "$word\n"; }

  11. Split function (continued) • Split a string into words • Depending on how you define a word • We define that words are separated by non-word characters • “Hi there, Java---API!”is considered as four words string = “Hi there, Java---API!” ; @words = split(/\W+/, $string); • @words now: (Hi, there, Java, API)

  12. Task 2 • Write a Perl program that can • Convert the following string to an array of word • Convert upper case letters into lower case • Display each word in one line “Hello There, (It is) Nice to SEE Everyone!”

  13. Perl Program for Task 2 #!/usr/bin/perl $data="Hello There, (It is) Nice to SEE Everyone!"; $data =~ tr/[A-Z]/[a-z]/; @words=split(/\W+/, $data); foreach $word (@words) { print "$word\n"; }

  14. Hash • Key Value • Key must be unique • Examples: • Student-ID  Student GPA • Bank_account_num  Balance • Name  Age • Name  Birthday Student_ID Student_GPA

  15. Hash (red is key, blue is value) • Fill the hash %C151 = (1001 => 3.5, 4004 => 3.8, 3003 => 3.5); • Copy a hash %CSCI_C151_ = %C151; • Lookup value through key $my_gpa = $C151{4004}; • Update hash value through key $C151{4004} = 4.0; • Insert a new entry (key->value) $C151{2002} = 3.0;

  16. Operations on Hash • keys HASH returns an array with only the keys in the hash. • values HASH returns an array with only the values in the hash, in the same order as the keys returned by keys. %C151 = (1001 => 3.5, 4004 => 3.8, 3003 => 3.5); @k = keys %C151; #@k now: (1001, 4004, 3003) @v = values %C151; #@v now: (3.5, 3.8, 3.5)

  17. Hash operations • Display all pairs of keyvalue foreach $key (keys %hash) { print “$key ---> $hash{$key}\n"; }

  18. Hash operations • Display the all the pairs of keyvalue in ascending order of keys (alphabetically). foreach $key (sort keys %hash) { print “$key ---> $hash{$key}\n"; }

  19. Task 3 (What Will Be Displayed) #!/usr/bin/perl %C151 = (1001=>3.5, 4004=>3.8, 3003=>3.5); print "the GPA of ID(3003) is $C151{3003}\n"; $C151{4004} = 4.0; $C151{2002} = 3.0; foreach $ID (keys %C151) { print "$ID-->$C151{$ID}\n"; } print "\nSort By ID:\n"; foreach $ID (sort (keys %C151)) { print "$ID-->$C151{$ID}\n"; }

  20. Task 3 #!/usr/bin/perl %C151 = (1001=>3.5, 4004=>3.8, 3003=>3.5); print "the GPA of ID(3003) is $C151{3003}\n"; $C151{4004} = 4.0; $C151{2002} = 3.0; foreach $ID (keys %C151) { print "$ID-->$C151{$ID}\n"; } print "\nSort By ID:\n"; foreach $ID (sort (keys %C151)) { print "$ID-->$C151{$ID}\n"; }

  21. Mission Statement of IUSB • “Indiana University South Bend is the comprehensive undergraduate and graduate regional campus of Indiana University that is committed to serving north central Indiana and southwestern Michigan. Its mission is to create, disseminate, preserve, and apply knowledge. The campus is committed to excellence in teaching, learning, research, and creative activity; to strong liberal arts and sciences programs and professional disciplines; to acclaimed programs in the arts and nursing/health professions; and to diversity, civic engagement, and a global perspective. IU South Bend supports student learning, access and success for a diverse residential and non-residential student body that includes under-represented and international students. The campus fosters student-faculty collaboration in research and learning.  Committed to the economic development of its region and state, Indiana University South Bend meets the changing educational and research needs of the community and serves as a vibrant cultural resource.” • (http://www.chancellor.iusb.edu/missionstatement.shtml)

  22. Task 4 • Analyze the Mission Statement of IUSB and display • Number of words (ignore case differences) • Number of unique words (ignore case differences) • Count of each unique word in ascending order of words (alphabetically)

  23. Script for Task 4 #!/usr/bin/perl $statement = “….” #replace …. With mission statement print "$statement\n"; $statement =~ tr/A-Z/a-z/; @words = split(/\W+/, $statement); # count the words, %wct is a hashtable foreach $word (@words) { $wct{$word} = $wct{$word} + 1; } @unique_word = keys %wct;

  24. Script for Task 4 (continued) $num_words = $#words + 1; # number of words $uni_words = $#unique_word + 1; # number of unique words print "The number of words is $num_words\n"; print "The number of unique words is $uni_words\n"; print "The occurrence of each words is listed below\n"; print "****************************************\n"; foreach $word (sort (keys %wct)) { print "$word\t$wct{$word}\n"; }

  25. In Previous Script • Keys are sorted alphabetically in ascending order, which is same as: foreach $word (sort {$a cmp $b} (keys %wct)) { print "$word\t$wct{$word}\n"; } • In descending order of words: foreach $word (sort {$b cmp $a} (keys %wct)) { print "$word\t$wct{$word}\n"; }

  26. How to Sort Hash by Values foreach $word (sort {$wct{$a} <=> $wct{$b}} (keys %wct)) { print "$word\t$wct{$word}\n"; } • Note: Switch a and b to change sorting order from ascending to descending

  27. Task 5 • Analyze the Mission Statement of IUSB and display • Number of words (ignore case differences) • Number of unique words (ignore case differences) • Count of each unique words in ascending order of counts

  28. Script for Task 5 #!/usr/bin/perl $statement = “….” #replace …. With mission statement print "$statement\n"; $statement =~ tr/A-Z/a-z/; @words = split(/\W+/, $statement); # count the words, %wct is a hashtable foreach $word (@words) { $wct{$word} = $wct{$word} + 1; } @unique_word = keys %wct;

  29. Script for Task 5 (continued) $num_words = $#words + 1; # number of words $uni_words = $#unique_word + 1; # number of unique words print "The number of words is $num_words\n"; print "The number of unique words is $uni_words\n"; print "The occurrence of each words is listed below\n"; print "****************************************\n"; foreach $word (sort {$wct{$a} <=>$wct{$b}} (keys %wct)) { print "$word\t$wct{$word}\n"; }

  30. How about in Descending Order of Count? • Change the sort function: foreach $word (sort {$wct{$b} <=>$wxt{$a}} (keys %wct)) { print "$word\t$wct{$word}\n"; }

  31. Reading Assignment • Chapter 11

More Related