Download presentation
Presentation is loading. Please wait.
Published byDavid Cockayne Modified over 9 years ago
1
Online Counseling Resource YCMOU ELearning Drive… School of Architecture, Science and Technology Yashwantrao Chavan Maharashtra Open University, Nashik – 422222, India
2
OC-SBI083-CP2-04 Introduction Programmes and Courses SEP – SBI083-CP2-04
3
School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.3 Credits Academic Inputs by Sonali Alkari MSc (Botany), P.G. D.C. Bio-Informatics sonalisa_alkari@yahoo.com
4
School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.4 How to Use This Resource Counselor at each study center should use this presentation to deliver lecture of 40-60 minutes during Face-To-Face counseling. Discussion about students difficulties or tutorial with assignments should follow the lecture for about 40-60 minutes. Handouts (with 6 slides on each A4 size page) of this presentation should be provided to each student. Each student should discuss on the discussion forum all the terms which could not be understood. This will improve his writing skills and enhance knowledge level about topics, which shall be immensely useful for end exam. Appear several times, for all the Self-Tests, available for this course. Student can use handouts for last minutes preparation just before end exam.
5
School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.5 Learning Objectives After studying this module, you should be able to: Describe regular expression Perform Pattern matching. Describe metacharacters and applications. Explain the split and join Functions
6
School of Science and Technology, Online Counseling Resource… Regular Expression-1 A regular expression is a pattern - a template - to be matched against a string. Matching a regular expression against a string either succeeds or fails. In perl, expressions bounded by two forward slashes // are called regular expressions or regexs. They are nothing but patterns that are compared to extract specific bits of information that the user want. If you will want to take a matched pattern and replace it with another string, parts of which may depend on exactly how and where the regular expression matched. © 2007, YCMOU. All Rights Reserved.6
7
School of Science and Technology, Online Counseling Resource… Regular Expression-2 Each program has a different set of (mostly overlapping) template characters. Perl is a semantic superset of all of these tools: any regular expression that can be described in one of these tools can also be written in Perl, but not necessarily using exactly the same characters. Regular expression easily manipulate strings of all sorts, such as DNA and protein sequence data. Some regular expression are very simple where some regular expression can be more complex. Regular expression are ways of matching one or more strings using wildcard like operator. © 2007, YCMOU. All Rights Reserved.7
8
School of Science and Technology, Online Counseling Resource… Regular Expression-3 Regular expression are interesting, important, and rich in capabilities. Perl makes particularly good use of regular expression, and perl documentation explain them well. Regular expression are useful when programming with biological data such as sequence, or with Genbank, PDB and Blast files. Regular expressions are ways of representing and searching for many strings with one string. Perl has many regular expression features. They are basically shortcuts for three fundamental ideas – repetition, alteration and concatenation. © 2007, YCMOU. All Rights Reserved.8
9
School of Science and Technology, Online Counseling Resource… Regular Expression & Pattern Regular expression describe patterns in strings. The pattern described by a single regular expression may match different string. Regular expression are used in pattern matching, that is, when you look to see if a certain patterns exists in a string. They also change strings, as with the/// operator that substitutes the patterns, if found, for replacement. Additionally, they are used in the tr function that can transliterate several character into replacement character through string. Regular expression are case-sensitive, unless explicitly told otherwise. © 2007, YCMOU. All Rights Reserved.9
10
School of Science and Technology, Online Counseling Resource… Example The = ~ Operator binds a pattern match to a string. /abc/ is the pattern abc, enclosed in forward slashes// to indicate that it’s regular expression pattern. $& is set to the matched pattern, if any. $alphabet= ‘abcdefghijklmnopqurstuvwxyz’; If ($alphabet = ~ /abc/) { Print $&; } In this case, the match succeeds, since ‘abc’ appears in thestring $alphabet, and the code just given prints out abc. © 2007, YCMOU. All Rights Reserved.10
11
School of Science and Technology, Online Counseling Resource… Regular Expression and Concatenation There are three basic ideas behind regular expressions. The first is concatenation: two items next to each other in a regular expression pattern (that’s the string between the forward slashes// in the example.) must match two items next to each other in the string being matched (the $alphabet in the examples). So to match ‘abc’ followed by ‘def’, concatenate them in regular expression: $alphabet= ‘abcdefghijklmnopqurstuvwxyz’; If ($alphabet = ~ /abcdef/) { Print $&; } This prints: abcdef © 2007, YCMOU. All Rights Reserved.11
12
School of Science and Technology, Online Counseling Resource… Regular Expression and Alteration The second major idea behind regular expression is alteration. Items separates by the | metacharacters match any one of the items. For example: $alphabet= ‘abcdefghijklmnopqurstuvwxyz’; If ($alphabet = ~ /a (b|c|d)c/) { Print $&; } The example shows how parentheses group things in a regular expression. The parentheses are metcharacters that aren’t matched in string; rather, they group the alteration, given as b|c|d, meaning any one of b, c or D at that position in the pattern © 2007, YCMOU. All Rights Reserved.12
13
School of Science and Technology, Online Counseling Resource… Metacharacters -1 \| () [ { ^ $ * ?. are the characters. Escaping with \ : A blackslash \ before a metacharacter causes it to match itself; for instance,\\ matches a single \ in the string. Alteration with |: the pipe | indicates alteration. Grouping with ():The parentheses ( ) provide grouping. Beginning and end of the string with ^ and $: the ^ metacharacters doesn’t match a character, rather, it asserts that the item that follows must be at the beginning of the string. Similarly, the $ metacharacters doesn’t match a character but asserts that the item that precedes it must be at the end of the string. © 2007, YCMOU. All Rights Reserved.13
14
School of Science and Technology, Online Counseling Resource… Metcharacters-2 Matching any character with.: The period or dot. Represents any character except newline. Quantifiers * + {MIN,} { MIN, MAX}? : These metachracters indicate the repetition of an item. The * metacharacter indicates one or more of the preceding item. The + metachractera indicates one or more of the preceding item. The {} brace mtachracters let you specify exactly three of the preceding item; {3, 7] means three, four, five, six and seven of the preceding item; and {3, } means three or more of the preceding item. The matches ? None or one of the preceding item. © 2007, YCMOU. All Rights Reserved.14
15
School of Science and Technology, Online Counseling Resource… Metcharacters-3 Character Classes: square brackets [] specify a character class. A character class matches class matches one character, which can be any specific character specified. For instance, [abc] matches either a, or b, or C at that position ( so it’s the same as a|b|c). A-Z is a range that matches any uppercase letter, a-z matches any lowercase letter, and 0-9 matches any digit. © 2007, YCMOU. All Rights Reserved.15
16
School of Science and Technology, Online Counseling Resource… Regular Expression & Repetition The third major idea of regular expression is repetition (or closure). This is indicated in a pattern with the quantifier metacharacters* called Kleene star after one of the inventors of regular expression. When * appears after an item, it means that the item may appear 0,1 or any number of times at that place in the string. So, for example, all of the following pattern matches will succeeds; ‘AC’= ~ /AB*C/; ‘ABC’= ~ /AB*C/; ‘ABBBBBBBBBC’= ~ /AB*C/; © 2007, YCMOU. All Rights Reserved.16
17
School of Science and Technology, Online Counseling Resource… regex Operators symbolfunction //[Pattern delimiter] =~[Binding operator] !~[Negative operator] s///[Substitution operator] Tr///[Translation operator] © 2007, YCMOU. All Rights Reserved.17 Beginning of pattern and end of pattern anchors ^ or /A[ match at the beginning of string] \b[match at beginning or end of word]
18
School of Science and Technology, Online Counseling Resource… Pattern Modifier & Conditional Matching Operator Pattern modifier operators symbolfunction g[enable global substituitions ] i[enable case insensitive match] l[enable matching a list of patterns in ()] m[treat pattern as multiple line] Ts[Treat pattern as single line] © 2007, YCMOU. All Rights Reserved.18 conditional matching operator symbolFunction ?=[conditional positive matching] =~[conditional negative matching]
19
School of Science and Technology, Online Counseling Resource… Match Quantifiers Match quantifiers +[match one or more instance of a given pattern ] ?[match zero or one instance of a given pattern] © 2007, YCMOU. All Rights Reserved.19 Match quantifiers can be represented in the form {n, m} where n[minimum number of times a pattern is to be matched]
20
School of Science and Technology, Online Counseling Resource… Special Characters Special characters.[match any single character except \n ] \D[match any single non-digit] \S[Match any single non-white space ] \W[Match any single non-word ] © 2007, YCMOU. All Rights Reserved.20
21
School of Science and Technology, Online Counseling Resource… Substitutions-1 We've already talked about the simplest form of the substitution operator: s/old-regex/new-string/. If you want the replacement to operate on all possible matches instead of just the first match, append a g to the substitution, as in: $_ = "foot fool buffoon";s/foo/bar/g; # $_ is now "bart barl bufbarn" The replacement string is variable interpolated, allowing you to specify the replacement string at run-time: $_ = "hello, world"; $new = "goodbye"; s/hello/$new/; © 2007, YCMOU. All Rights Reserved.21
22
School of Science and Technology, Online Counseling Resource… Substitutions-2 # replaces hello with goodbye Pattern characters in the regular expression allow patterns to be matched, rather than just fixed characters: $_ = "this is a test"; s/(\w+)/ /g; # $_ is now " “ As with the match operator, an alternate delimiter can be selected if the slash is inconvenient. Just use the same character three times: s#fred#barney#; # replace fred with barney, like s/fred/barney/ © 2007, YCMOU. All Rights Reserved.22
23
School of Science and Technology, Online Counseling Resource… Substitutions-3 Also as with the match operator, you can specify an alternate target with the =~ operator. In this case, the selected target must be something you can assign a scalar value to, such as a scalar variable or an element of an array. Here's an example: $which = "this is a test"; $which =~ s/test/quiz/; # $which is now "this is a quiz“ $someplace[$here] =~ s/left/right/; # change an array element $d{"t"} =~ s/^/x /; # prepend "x " to hash element © 2007, YCMOU. All Rights Reserved.23
24
School of Science and Technology, Online Counseling Resource… The Split and Join Functions-1 Regular expressions can be used to break a string into fields. The split function breaks, and the join function glues the pieces back together. The split function takes a regular expression and a string, and looks for all occurrences of the regular expression within that string. The parts of the string that don't match the regular expression are returned in sequence as a list of values. To split the data in the job file into discreate container is accomplish with perl’s split function. © 2007, YCMOU. All Rights Reserved.24
25
School of Science and Technology, Online Counseling Resource… The Split and Join Functions-2 $line ="merlyn::118:10:Randal:/home/merlyn:/usr/bin/perl"; @fields = split(/:/,$line); # split $line, using : as delimiter # now @fields is ("merlyn","","118","10","Randal",# "/home/merlyn","/usr/bin/perl") Note how the empty second field became an empty string. If you don't want this, match all of the colons in one fell swoop: @fields = split(/:+/, $line); This matches one or more adjacent colons together, so there is no empty second field. One common string to split is the $_ variable, and that turns out to be the default: $_ = "some string"; @words = split(/ /); # same as @words = split(/ /, $_); © 2007, YCMOU. All Rights Reserved.25
26
School of Science and Technology, Online Counseling Resource… The Split and Join Functions-3 For this split, consecutive spaces in the string to be split will cause null fields (empty strings) in the result. A better pattern would be / +/, or ideally /\s+/, which matches one or more whitespace characters together. In fact, this pattern is the default pattern, so if you're splitting the $_ variable on whitespace, you can use all the defaults. The " " string is the default pattern, and this will cause leading whitespace to be ignored. @words = split; # same as @words = split(/\s+/, $_); © 2007, YCMOU. All Rights Reserved.26
27
School of Science and Technology, Online Counseling Resource… The Split and Join Functions-4 The join function takes a list of values and glues them together with a glue string between each list element. It looks like this: $bigstring = join($glue,@list); For example, to rebuild the password line, try something like: $outline = join(":", @fields); Note that the glue string is not a regular expression - just an ordinary string of zero or more characters. If you need to get glue ahead of every item instead of just between items, a simple cheat suffices: © 2007, YCMOU. All Rights Reserved.27
28
School of Science and Technology, Online Counseling Resource… The Split and Join Functions-5 $result = join ("+", "", @fields); Here, the extra "" is treated as an empty element, to be glued together with the first data element of @fields. This results in glue ahead of every element. Similarly, you can get trailing glue with an empty element at the end of the list, like so: $output = join ("\n", @data, ""); © 2007, YCMOU. All Rights Reserved.28
29
School of Science and Technology, Online Counseling Resource… The Split and Join Functions-6 $result = join ("+", "", @fields); Here, the extra "" is treated as an empty element, to be glued together with the first data element of @fields. This results in glue ahead of every element. Similarly, you can get trailing glue with an empty element at the end of the list, like so: $output = join ("\n", @data, ""); © 2007, YCMOU. All Rights Reserved.29
30
School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.30 What You Learn… A regular expression is a pattern - a template - to be matched against a string. In perl, expressions bounded by two forward slashes // are called regular expressions or regexs. Concatenation, Alteration, repetition are three basic ideas behind regular expressions. \| () [ { ^ $ * ?. are the metacharacters. you can specify an alternate target with the =~ operator, called as substution operator. Regular expressions can be used to break a string into fields. The split function breaks, and the join function glues the pieces back together.
31
School of Science and Technology, Online Counseling Resource… Critical Thinking Questions 1.Describe pattern matching in details. 2.Describe metacharacters and their application in details. 3.Write a short note on split and join function. 4.Describe Substitutions in perl. © 2007, YCMOU. All Rights Reserved.31
32
School of Science and Technology, Online Counseling Resource… Hints For Critical Thinking Question 1.Describe Concatenation, Alteration, repetition ideas behind regular expressions. 2. \| () [ { ^ $ * ?. are the metacharacters and their applications in perl programming. 3.The split function breaks, and the join function glues the pieces back togetherand example. 4.you can specify an alternate target with the =~ operator, called as substution operator. © 2007, YCMOU. All Rights Reserved.32
33
School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.33 Study Tips:Books Beginning Perl Beginning Perl Simon Cozens, Peter Wainwright. 700 pages. Wrox Press Inc. (May 25, 2000). Impatient Perl Greg London (Feb 7, 2004). Impatient Perl Extreme Perl Robert Nagler Extreme Perl MacPerl: Power & Ease Vicky Brown and Chris Nandor. (1998). MacPerl: Power & Ease Picking Up Perl Bradley M. Kuhn and Neil Smyth. self published. second edition. (July 2005). Picking Up Perl
34
School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.34 Study Tips:Web Resources www.en.wikipedia.org Microsoft Encarta Encyclopedia http://www.perl.org/ http://en.wikibooks.org/wiki/Programming:Perl_Websites http://cpan.perl.org/
35
School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.35 Community Web Sites Planet Perl - an aggregation of selected perl journals Planet Perl use Perl; - read community news and personal journals, or start your own journal use Perl; Perl Monks - the wisdom of the Monks can guide you on your Perl Quests Perl Monks perl.org - your current location perl.org the Perl Apprenticeship Site the Perl Apprenticeship Site
36
School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.36 End of the Presentation Thank You
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.