1 perl regular expressions. things perl can do easily with regular expression 2 pattern matching...

31
1 Perl Regular Expressions

Upload: darleen-thomas

Post on 18-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

1

Perl Regular Expressions

Page 2: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

Things Perl Can Do Easily with Regular Expression

2

Pattern matchingFind 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 functionSplit a string into substrings

Page 3: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

Pattern Matchingif ($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”;};

Page 4: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

Pattern Matching to Verify Email@mail = ('[email protected]‘,'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:[email protected] seems to be a good eMail addresshab&cnn.com is a bad address

Page 5: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

SubstitutionString substitionIn string $string, replace every "Bill Clinton"

with an “Bush”:$string =~ s/Bill Clinton/Bush/;

Page 6: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

TranslationReplace one character with another

characterIn $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]/;

Page 7: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

Split functionSplit 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);

Page 8: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

Wildcard Character and RepetitionWildcard 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

Page 9: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

Task 1Write a Perl program that can

Convert the following string to an array of word

Convert upper case letters into lower caseDisplay each word in one line

“Hello There Nice to SEE Everyone”

Page 10: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

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";}

Page 11: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

Split function (continued)Split a string into wordsDepending on how you define a wordWe 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)

Page 12: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

Task 2Write a Perl program that can

Convert the following string to an array of word

Convert upper case letters into lower caseDisplay each word in one line

“Hello There, (It is) Nice to SEE Everyone!”

Page 13: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

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";}

Page 14: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

HashKey ValueKey must be uniqueExamples:

Student-ID Student GPABank_account_num BalanceName AgeName Birthday

1001 3.5

4004 3.8

3003 3.5

Student_ID Student_GPA

Page 15: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

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;

Hash (red is key, blue is value)

1001 3.5

4004 4.0

3003 3.5

2002 3.0

1001 3.5

4004 3.8

3003 3.5

Page 16: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

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)

Operations on Hash

Page 17: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

Display all pairs of keyvalue

foreach $key (keys %hash){

print “$key ---> $hash{$key}\n"; }

Hash operations

Page 18: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

Display the all the pairs of keyvalue in ascending order of keys (alphabetically).

foreach $key (sort keys %hash){

print “$key ---> $hash{$key}\n"; }

Hash operations

Page 19: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

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";}

Page 20: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

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";}

Page 21: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

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)

Page 22: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

Task 4Analyze the Mission Statement of IUSB and

displayNumber of words (ignore case differences)Number of unique words (ignore case

differences)Count of each unique word in ascending

order of words (alphabetically)

Page 23: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

Script for Task 4#!/usr/bin/perl$statement = “….” #replace …. With mission

statementprint "$statement\n";

$statement =~ tr/A-Z/a-z/;@words = split(/\W+/, $statement);

# count the words, %wct is a hashtableforeach $word (@words){

$wct{$word} = $wct{$word} + 1; }

@unique_word = keys %wct;

Page 24: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

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";}

Page 25: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

In Previous ScriptKeys 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";}

Page 26: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

How to Sort Hash by Valuesforeach $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

Page 27: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

Task 5Analyze the Mission Statement of IUSB and

displayNumber of words (ignore case differences)Number of unique words (ignore case differences)Count of each unique words in ascending order of

counts

Page 28: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

Script for Task 5#!/usr/bin/perl$statement = “….” #replace …. With mission

statementprint "$statement\n";

$statement =~ tr/A-Z/a-z/;@words = split(/\W+/, $statement);

# count the words, %wct is a hashtableforeach $word (@words){

$wct{$word} = $wct{$word} + 1; }

@unique_word = keys %wct;

Page 29: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

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} <=>$wxt{$b}} (keys %wct))

{print "$word\t$wct{$word}\n";

}

Page 30: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

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";}

Page 31: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern

Reading AssignmentChapter 11