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

Slides:



Advertisements
Similar presentations
Chapter 25 Perl and CGI (Common Gateway Interface)
Advertisements

Programming and Perl for Bioinformatics Part III.
ISBN Regular expressions Mastering Regular Expressions by Jeffrey E. F. Friedl –(on reserve.
Engineering Computing I Chapter 1 – Part B A Tutorial Introduction continued.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
Scripting Languages Chapter 5 Hashes. Hash Data structure, not unlike an array – it will hold any number of values It indexes values by name – not by.
ISBN Chapter 6 Data Types Character Strings Pattern Matching.
PERL Part 3 1.Subroutines 2.Pattern matching and regular expressions.
Regular Expressions Regular Expression (or pattern) in Perl – is a template that either matches or doesn’t match a given string. if( $str =~ /hello/){
W3101: Programming Languages (Perl) 1 Perl Regular Expressions Syntax for purpose of slides –Regular expression = /pattern/ –Broader syntax: if (/pattern/)
UNIX Filters.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Regular Expression A regular expression is a template that either matches or doesn’t match a given string.
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
Chapter 06: Lecture Notes (CSIT 104) 1 Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 1 Copyright © 2008 Prentice-Hall. All rights reserved.
Chapter 9 Creating Formulas that Manipulate Text Microsoft Office Excel 2003.
Last Updated March 2006 Slide 1 Regular Expressions.
1 An Introduction to Perl Part 2 CSC8304 – Computing Environments for Bioinformatics - Lecture 8.
Sort the Elements of an Array Using the ‘sort’ keyword, by default we can sort the elements of an array lexicographically. Elements considered as strings.
1 Perl Perl basics Perl Elements Arrays and Hashes Control statements Operators OOP in Perl.
Computer Programming for Biologists Class 5 Nov 20 st, 2014 Karsten Hokamp
Perl and Regular Expressions Regular Expressions are available as part of the programming languages Java, JScript, Visual Basic and VBScript, JavaScript,
Introduction To Perl Susan Lukose. Introduction to Perl Practical Extraction and Report Language Easy to learn and use.
Programming in Perl regular expressions and m,s operators Peter Verhás January 2002.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2015, Fred McClurg, All Rights.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Perl Language Yize Chen CS354. History Perl was designed by Larry Wall in 1987 as a text processing language Perl has revised several times and becomes.
Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 1 Regular Expressions.
Chapter 9: Perl (continue) Advanced Perl Programming Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
Overview A regular expression defines a search pattern for strings. Regular expressions can be used to search, edit and manipulate text. The pattern defined.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1.
12. Regular Expressions. 2 Motto: I don't play accurately-any one can play accurately- but I play with wonderful expression. As far as the piano is concerned,
CSC 4630 Meeting 21 April 4, Return to Perl Where are we? What is confusing? What practice do you need?
5 1 Data Files CGI/Perl Programming By Diane Zak.
CS 330 Programming Languages 10 / 02 / 2007 Instructor: Michael Eckmann.
CS 330 Class 9 Programming plan for today: More of how data gets into a script Via environment variables Via the url From a form By editing the url directly.
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
LING/C SC/PSYC 438/538 Lecture 8 Sandiway Fong. Adminstrivia Homework 4 not yet graded …
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Regular Expressions.
CPTG286K Programming - Perl Chapter 1: A Stroll Through Perl Instructor: Denny Lin.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
Basic Variables & Operators Web Programming1. Review: Perl Basics Syntax ► Comments: start with # (ignored by Perl) ► Statements: ends with ; (performed.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Standard Types and Regular Expressions CS 480/680 – Comparative Languages.
Senate Planning Review Committee November 24, 2014.
An Introduction to Regular Expressions Specifying a Pattern that a String must meet.
8 1 String Manipulation CGI/Perl Programming By Diane Zak.
CSC 4630 Meeting 17 March 21, Exam/Quiz Schedule Due to ice, travel, research and other commitments that we all have: –Quiz 2, scheduled for Monday.
Finding substrings my $sequence = "gatgcaggctcgctagcggct"; #Does this string contain a startcodon? if ($sequence =~ m/atg/) { print "Yes"; } else { print.
-Joseph Beberman *Some slides are inspired by a PowerPoint presentation used by professor Seikyung Jung, which was derived from Charlie Wiseman.
The Scripting Programming Language
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
Strings in Python String Methods. String methods You do not have to include the string library to use these! Since strings are objects, you use the dot.
Filters and Utilities. Notes: This is a simple overview of the filtering capability Some of these commands are very powerful ▫Only showing some of the.
CS 330 Class 7 Comments on Exam Programming plan for today:
Lecture 19 Strings and Regular Expressions
Perl-Compatible Regular Expressions Part 1
Lecture 9 Shell Programming – Command substitution
LING/C SC/PSYC 438/538 Lecture 8 Sandiway Fong.
Perl Variables: Array Web Programming.
Perl Variables: Hash Web Programming.
LING/C SC/PSYC 438/538 Lecture 10 Sandiway Fong.
PERL: part II hashes, foreach control statements, and the split function By: Kevin Walton.
Introduction to Computer Science
Regular Expression: Pattern Matching
LING/C SC/PSYC 438/538 Lecture 12 Sandiway Fong.
Presentation transcript:

1 Perl Regular Expressions

Things Perl Can Do Easily with Regular Expression 2 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

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

Pattern Matching to Verify = foreach $address { if($address =~ { print “$address seems to be a good address\n"; } else { print "$address is a bad address\n"; } OUTPUT: seems to be a good address hab&cnn.com is a bad address

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

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

Split function Split a string into substrings Split on character: $data = 'Becky Lincoln,25,female,South = split(/,/, $data); Split on string: $data = ‘Care = split(/~~~/, $data);

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

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”

Perl Program for Task 1 #!/usr/bin/perl $data="Hello There Nice to SEE Everyone"; $data =~ $data); /, $data); foreach $word { print "$word\n"; }

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!” = split(/\W+/, now: (Hi, there, Java, API)

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!”

Perl Program for Task 2 #!/usr/bin/perl $data="Hello There, (It is) Nice to SEE Everyone!"; $data =~ $data); foreach $word { print "$word\n"; }

Hash Key  Value Key must be unique Examples: Student-ID  Student GPA Bank_account_num  Balance Name  Age Name  Birthday Student_IDStudent_GPA

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)

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 => = keys %C151; now: (1001, 4004, = values %C151; now: (3.5, 3.8, 3.5) Operations on Hash

Display all pairs of key  value foreach $key (keys %hash) { print “$key ---> $hash{$key}\n"; } 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"; } Hash operations

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

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

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

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)

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

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

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

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

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

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

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

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

Reading Assignment Chapter 11