1 Perl, Beyond the Basics: Regular Expressions, Subroutines, and Objects in Perl CSCI 431 Programming Languages Fall 2003.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Regular Expressions in Perl By Josue Vazquez. What are Regular Expressions? A template that either matches or doesn’t match a given string. Often called.
Programming and Perl for Bioinformatics Part III.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
ISBN Regular expressions Mastering Regular Expressions by Jeffrey E. F. Friedl –(on reserve.
Asp.NET Core Vaidation Controls. Slide 2 ASP.NET Validation Controls (Introduction) The ASP.NET validation controls can be used to validate data on the.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
COS 381 Day 19. Agenda  Assignment 5 Posted Due April 7  Exam 3 which was originally scheduled for Apr 4 is going to on April 13 XML & Perl (Chap 8-10)
ISBN Chapter 6 Data Types Character Strings Pattern Matching.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 10 / 10 / 2006 Instructor: Michael Eckmann.
CS 330 Programming Languages 09 / 30 / 2008 Instructor: Michael Eckmann.
Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to.
More Regular Expressions. List/Scalar Context for m// Last week, we said that m// returns ‘true’ or ‘false’ in scalar context. (really, 1 or 0). In list.
Regular Expressions. What are regular expressions? A means of searching, matching, and replacing substrings within strings. Very powerful (Potentially)
Subroutines. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc We’ll just say Subroutines. –“Functions” generally means built-in.
Scripting Languages Chapter 8 More About Regular Expressions.
Regular Expressions in ColdFusion Applications Dave Fauth DOMAIN technologies Knowledge Engineering : Systems Integration : Web.
Last Updated March 2006 Slide 1 Regular Expressions.
Lecture 7: Perl pattern handling features. Pattern Matching Recall =~ is the pattern matching operator A first simple match example print “An methionine.
Programming Perl in UNIX Course Number : CIT 370 Week 4 Prof. Daniel Chen.
 Text Manipulation and Data Collection. General Programming Practice Find a string within a text Find a string ‘man’ from a ‘A successful man’
Computer Programming for Biologists Class 5 Nov 20 st, 2014 Karsten Hokamp
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormack 3rd floor 607.
Regular Expressions in Perl Part I Alan Gold. Basic syntax =~ is the matching operator !~ is the negated matching operator // are the default delimiters.
Regular Expressions Regular expressions are a language for string patterns. RegEx is integral to many programming languages:  Perl  Python  Javascript.
Perl and Regular Expressions Regular Expressions are available as part of the programming languages Java, JScript, Visual Basic and VBScript, JavaScript,
Python Regular Expressions Easy text processing. Regular Expression  A way of identifying certain String patterns  Formally, a RE is:  a letter or.
1 CSC 594 Topics in AI – Text Mining and Analytics Fall 2015/16 4. Document Search and Regular Expressions.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
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.
Regular Expressions in PHP. Supported RE’s The most important set of regex functions start with preg. These functions are a PHP wrapper around the PCRE.
Overview A regular expression defines a search pattern for strings. Regular expressions can be used to search, edit and manipulate text. The pattern defined.
Module 6 – Generics Module 7 – Regular Expressions.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming regular expressions.
Regular Expressions in Perl CS/BIO 271 – Introduction to Bioinformatics.
Regular Expressions What is this line all about? while (!($search =~ /^\s*$/)) { It’s a string search just like before, but with a huge twist – regular.
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,
CS346 Regular Expressions1 Pattern Matching Regular Expression.
GREP. Whats Grep? Grep is a popular unix program that supports a special programming language for doing regular expressions The grammar in use for software.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
CS 330 Programming Languages 10 / 02 / 2007 Instructor: Michael Eckmann.
R EGULAR E XPRESSION IN P ERL (P ART 1) Thach Nguyen.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
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 …
C++ Basics Tutorial 5 Constants. Topics Covered Literal Constants Defined Constants Declared Constants.
Karthik Sangaiah.  Developed by Larry Wall ◦ “There’s more than one way to do it” ◦ “Easy things should be easy and hard things should be possible” 
Controlling Computers with Programs When you create a computer program you are creating a set of instructions that tell the computer exactly and completely.
Chapter 4 © 2009 by Addison Wesley Longman, Inc Pattern Matching - JavaScript provides two ways to do pattern matching: 1. Using RegExp objects.
CSCI 1100/1202 January 14, Abstraction An abstraction hides (or ignores) the right details at the right time An object is abstract in that we don't.
CS 330 Programming Languages 09 / 30 / 2008 Instructor: Michael Eckmann.
Regular Expressions Copyright Doug Maxwell (
Definition of the Programming Language CPRL
Looking for Patterns - Finding them with Regular Expressions
CSC 594 Topics in AI – Natural Language Processing
Regular Expressions in Perl
Regular Expressions and perl
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
LING/C SC/PSYC 438/538 Lecture 8 Sandiway Fong.
CSC 594 Topics in AI – Natural Language Processing
Subroutines Web Programming.
CSCI 431 Programming Languages Fall 2003
Functions, Regular expressions and Events
C Programming Getting started Variables Basic C operators Conditionals
- Regular expressions:
Regular Expression: Pattern Matching
ADVANCE FIND & REPLACE WITH REGULAR EXPRESSIONS
Perl Regular Expressions – Part 1
Presentation transcript:

1 Perl, Beyond the Basics: Regular Expressions, Subroutines, and Objects in Perl CSCI 431 Programming Languages Fall 2003

2 Regular Expressions Perl provides a great deal of built-in text-processing functions. We will cover only some of the most popular such functions.Perl provides a great deal of built-in text-processing functions. We will cover only some of the most popular such functions. Perl uses forward slashes to delimit regular expressions for pattern matching and substitution.Perl uses forward slashes to delimit regular expressions for pattern matching and substitution. Strings are evaluated to true of false via the =~ operator.Strings are evaluated to true of false via the =~ operator. $a = "Mary had a little lamb"; $a =~/little/ # evaluates to true $a =~/brittle/ # evaluates to false

3 Patten Matching Perl provides a set of modifying characters for string matching, some of these are shown below:Perl provides a set of modifying characters for string matching, some of these are shown below: Modifier Meaning Modifier Meaning i matches characters regardless of case s treats string as a single line gglobally find all matches e.g.: $a =~/little/i

4 Example while ($line = ) { if ($line =~ / { print $line; }}or while( ) { print if / }

5 Pattern Matching Perl uses a set of meta-characters to extend the functionality of pattern matching. Below is a table of commonly used meta characters.Perl uses a set of meta-characters to extend the functionality of pattern matching. Below is a table of commonly used meta characters. Metacharacter Meaning. matches any single character except for \n. matches any single character except for \n ^ matches the front of a line ^ matches the front of a line $ matches the end of a line $ matches the end of a line *matches proceeded character 0 or more times *matches proceeded character 0 or more times + matches proceeded character 1 or more times + matches proceeded character 1 or more times ? matches proceeding character 0 or 1 times ? matches proceeding character 0 or 1 times [...] matches any of the class of characters [...] matches any of the class of characters

6 Pattern Matching Perl also has a set of special characters proceeded with a backslash, some of which are listed below.Perl also has a set of special characters proceeded with a backslash, some of which are listed below. Special Character Meaning \s any whitespace \S any non whitespace \d any digit i.e. [0-9] \w any alphanumeric i.e [a-zA-Z0-9] \n newline \t tab

7 Example from 3 rd Edition of Perl Programming (finding duplicate words) while (<>) { while (<>) { while ( m{ while ( m{ \b#start at a word boundary (\w\S+)#find a wordish chunk ( \s+#separated by whitespace \1#and that chunk again )+# repeat ad lib \b }xig }xig ){ print “dup word ‘$1’ at paragraph $.\n”; }}

8 Substitution Perl provides a simple way of searching for patterns and substituting new patterns in their place.Perl provides a simple way of searching for patterns and substituting new patterns in their place. This accomplished by using a s before a slash delimited regular expression.This accomplished by using a s before a slash delimited regular expression. s/string1/string2/i # replaces the first instance of string1 with # with string 2 the /i forces a case sensitive # search

9 Functions A subprogram in Perl is a function.A subprogram in Perl is a function. An ampersand is placed before the function name to denote invocation.An ampersand is placed before the function name to denote invocation. If the function takes arguments, they are placed within parentheses following the name of the function (the parentheses may be omitted).If the function takes arguments, they are placed within parentheses following the name of the function (the parentheses may be omitted).&aFunction(); Control is transferred to the code of the function and transferred back at the end of the code or when an explicit return operator is reached.Control is transferred to the code of the function and transferred back at the end of the code or when an explicit return operator is reached.

10 Function Definition The function definition is marked by the keyword sub followed by the name of the function (without an ampersand prefix). The function body is enclosed in curly brackets.The function definition is marked by the keyword sub followed by the name of the function (without an ampersand prefix). The function body is enclosed in curly brackets. sub aFunction { stmt_1;stmt_2;…stmt_n;} The value returned by a function is the value of the last expression evaluated.The value returned by a function is the value of the last expression evaluated. Functions can not be nested.Functions can not be nested.

11 Arguments The arguments to a function constitute a list. They are available within the function definition through the predefined list arguments to a function constitute a list. They are available within the function definition through the predefined list &aFunction ($a, “hello”, $b); sub aFunction { foreach $temp { print “$temp \n”; }}

12 Variable Scope Variables defined within the body of a Perl program are a available inside a Perl function as global variables.Variables defined within the body of a Perl program are a available inside a Perl function as global variables. Perl offers 3 scoping declarations:Perl offers 3 scoping declarations: –mye.g., my nose; –oure.g., our $house –local$local $Tvchannel my and local can both be used (although they work in different ways) to limit the scope of variables that are intended to be local to a function.my and local can both be used (although they work in different ways) to limit the scope of variables that are intended to be local to a function. Sub aFunction { my ($local1, $local2); …}

13 Object Orientated Programming in Perl Part 1: References and PackagesPart 1: References and PackagesPart 1: References and PackagesPart 1: References and Packages Part 2: Object Properties and Object MethodsPart 2: Object Properties and Object MethodsPart 2: Object Properties and Object MethodsPart 2: Object Properties and Object Methods