CSCI 431 Programming Languages Fall 2003

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Perl & Regular Expressions (RegEx)
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.
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.
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.
More on Regular Expressions Regular Expressions More character classes \s matches any whitespace character (space, tab, newline etc) \w matches.
Regular Expressions in ColdFusion Applications Dave Fauth DOMAIN technologies Knowledge Engineering : Systems Integration : Web.
Last Updated March 2006 Slide 1 Regular Expressions.
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.
Perl and Regular Expressions Regular Expressions are available as part of the programming languages Java, JScript, Visual Basic and VBScript, JavaScript,
Agenda Regular Expressions (Appendix A in Text) –Definition / Purpose –Commands that Use Regular Expressions –Using Regular Expressions –Using the Replacement.
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.
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.
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.
Module 6 – Generics Module 7 – Regular Expressions.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming regular expressions.
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.
CS 330 Programming Languages 10 / 02 / 2007 Instructor: Michael Eckmann.
R EGULAR E XPRESSION IN P ERL (P ART 1) Thach Nguyen.
1 Perl, Beyond the Basics: Regular Expressions, Subroutines, and Objects in Perl CSCI 431 Programming Languages Fall 2003.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
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” 
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607.
Prof. Alfred J Bird, Ph.D., NBCT Door Code for IT441 Students.
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
Tutorial On Lex & Yacc.
Regular Expressions and perl
Lecture 9 Shell Programming – Command substitution
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Scope, Objects, Strings, Numbers
Concepts of Programming Languages
CompSci 230 Software Construction
LING/C SC/PSYC 438/538 Lecture 8 Sandiway Fong.
CSC 594 Topics in AI – Natural Language Processing
Subroutines Web Programming.
PHP.
Functions, Regular expressions and Events
Statement-Level Control Structures
C Programming Getting started Variables Basic C operators Conditionals
Regular Expressions and Grep
- Regular expressions:
Regular Expression: Pattern Matching
ADVANCE FIND & REPLACE WITH REGULAR EXPRESSIONS
Perl Regular Expressions – Part 1
Presentation transcript:

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

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 uses forward slashes to delimit regular expressions for pattern matching and substitution. 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

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

Example while ($line = <FILE>) { if ($line =~ /http:/) { print $line; } or while(<FILE>) { print if /http:/;

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. Metacharacter Meaning . matches any single character except for \n ^ matches the front of a line $ matches the end of a line * matches proceeded character 0 or more times + matches proceeded character 1 or more times ? matches proceeding character 0 or 1 times [...] matches any of the class of characters

Pattern Matching 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

Example from 3rd Edition of Perl Programming (finding duplicate words) while (<>) { 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 ) { print “dup word ‘$1’ at paragraph $.\n”; }

Substitution 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. s/string1/string2/i # replaces the first instance of string1 with # with string 2 the /i forces a case sensitive # search

Functions A subprogram in Perl is a function. 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). &aFunction(); &aFunction($arg1, @arg2); 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.

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. sub aFunction { stmt_1; stmt_2; … stmt_n; } The value returned by a function is the value of the last expression evaluated. Functions can not be nested.

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

Variable Scope Variables defined within the body of a Perl program are a available inside a Perl function as global variables. Perl offers 3 scoping declarations: my e.g., my nose; our e.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. Sub aFunction { my ($local1, $local2); … }

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