Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CSCE 740 Software Engineering Ruby and the tools 740Tools03RubyRegExpr Topics January 22, 2014 Ruby Reg Expressions Ruby http://www.ruby-doc.org/ Starting-up http://ruby.about.com/od/tutorialsontheweb/tp/10waysfr ee.htm Rails and Beyond http://ruby.railstutorial.org/ruby-on-rails-tutorial-book –2– CSCE 740 Spring 2014 Ruby.new Really Object Oriented – everything is an object For Java you might use pos = Math.abs(num) In Ruby num = -123 pos = num.abs Variables inside literal strings #{ … } notation puts “the absolute value of #{num} is #{pos}\n” Variable Name Punctuation – 3 – name - local variable $name, $NAME, - globals (never use anyway!) @name – instance variables Prog Ruby 1.9 Dave Thomas @@name – class variables Name – class names and constants CSCE 740 Spring 2014 Puts_examples song = 1 sam = "" def sam.play(a) "duh dum, da dum de dum ..." end puts "gin joint".length puts "Rick".index("c") puts 42.even? puts sam.play(song) print “string with no newline” –4– Programming Ruby 1.9 Dave Thomas CSCE 740 Spring 2014 Method definition def say_goodnight(name) result = "Good night, " + name return result end # Time for bed... puts say_goodnight("John-Boy") puts say_goodnight("Mary-Ellen") –5– Programming Ruby 1.9 Dave Thomas CSCE 740 Spring 2014 CSE Linux Labs • 1D39 – Combination on secure site • • CSE home page – login with CEC credentials Computer resources/Computer labs • List of Linux workstations • IP addresses – machine names • Ifconfig // interface config IPv4 addr and Hardware=ethernet addr • “man keyword” online unix documentation –6– CSCE 740 Spring 2014 List of Ruby 3 Examples 1. Getting Started – “hello, Ruby Programmer” 2. Intro – a. b. c. d. e. f. Hello1 – def say_goodnight Puts examples Cmd_line – command line args passed into ruby program “arrays” – non-homogeneous hash_with_symbol_keys_19.rb – Weekdays – control structures – if-elseif-else example 3. Tutclasses- –7– CSCE 740 Spring 2014 google(ruby 1.9 tutorial) • Ruby Programming Language - ruby-lang.org/ • http://www.ruby-doc.org/ (again) • http://pragprog.com/titles/ruby3/source_code (again) • http://pragprog.com/book/ruby3/programming-ruby1-9 “Buy the book” page • • • Regular Expressions (download pdf) Namespaces, Source Files, and Distribution (download pdf) Ruby Library Reference • Built-in Classes and Modules (download pdf of the entry for class Array) • Standard Library • http://www.ruby-doc.org/stdlib-1.9.3/ –8– CSCE 740 Spring 2014 "gin joint".length » 9 "Rick".index("c") » 2 -1942.abs » 1942 sam.play(aSong) » "duh dum, da dum de dum ..." –9– Programming Ruby 1.9 Dave Thomas CSCE 740 Spring 2014 def sayGoodnight(name) result = "Goodnight, " + name return result end # Time for bed... puts sayGoodnight("John-Boy") puts sayGoodnight("Mary-Ellen") – 10 – Programming Ruby 1.9 Dave Thomas CSCE 740 Spring 2014 Simplifying def sayGoodnight(name) result = "Goodnight, #{name}“ return result end #Simplifying further eliminating the return statement def sayGoodnight(name) result = "Goodnight, #{name}“ end – 11 – Programming Ruby 1.9 Dave Thomas CSCE 740 Spring 2014 instSection['bassoon'] Arrays and Hashes a = [ 1, 'cat', 3.14 ] # array with three elements a[0] >> 1 >> [1, "cat", nil] a[2] = nil #dump array puts a empty1 = [] empty2 = Array.new a = %w{ ant bee cat dog elk } – 12 – Programming Ruby 1.9 Dave Thomas CSCE 740 Spring 2014 Hashes instSection = { 'cello' => 'string', 'clarinet' => 'woodwind', 'drum' 'oboe' In the online text “>>” means evaluates to instSection['oboe'] >> => 'percussion', => 'woodwind', instSection['cello'] >> 'trumpet' => 'brass', 'violin' => 'string’ instSection['bassoon'] >> } – 13 – Programming Ruby 1.9 Dave Thomas CSCE 740 Spring 2014 histogram = Hash.new(0) histogram['key1'] = histogram['key1'] + 1 – 14 – Programming Ruby 1.9 Dave Thomas CSCE 740 Spring 2014 Control Structures IF if count > 10 puts "Try again" elsif tries == 3 puts "You lose" else puts "Enter a number" end – 15 – // body http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 Control Structures While while weight < 100 and numPallets <= 30 pallet = nextPallet() weight += pallet.weight numPallets += 1 end – 16 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 More control puts "Danger, Will Robinson" if radiation > 3000 while square < 1000 square = square*square end More concisely, but readability?? square = square*square while square < 1000 – 17 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 Regular Expressions Regular expressions are expressions that specify collections of strings (formally languages) Main operators: Assume r and s are regular expressions then so are: r|s alternation denotes L(r) U L(s) rs concatenation which denotes L(r) L(s) r* Kleene closure zero or more occurrences of strings from L(r) concatenated Base regular expressions • strings are regexpr that match themselves,L(“ab”) = {“ab”} • the empty string ε is a regular expr L(ε) = {“”} – 18 – CSCE 740 Spring 2014 Ruby Regular Expressions [abc] A single character of: a, b or c [^abc] Any single character except: a, b, or c [a-z] Any single character in the range a-z [a-zA-Z] Any single character in the range a-z or A-Z ^ Start of line $ End of line \A Start of string \z End of string – 19 – http://rubular.com/ CSCE 740 Spring 2014 Ruby Regular Expressions (...) Capture everything enclosed (a|b) a or b a? Zero or one of a a* Zero or more of a a+ One or more of a a{3} Exactly 3 of a a{3,} 3 or more of a a{3,6} Between 3 and 6 of a – 20 – http://rubular.com/ CSCE 740 Spring 2014 Quoting First, always remember that you need to escape any of these characters with a backslash if you want them to be treated as regular characters to match: show_regexp('yes | no', /\|/) # => yes ->|<- no show_regexp('yes (no)', /\(no\)/) # => yes ->(no)<show_regexp('are you sure?', /e\?/) # => are you sur->e?<– 21 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 Matching Strings with Patterns Ruby operator =~ matches a string against a pattern. It returns the character offset into the string at which the match occurred: /cat/ =~ "dog and cat" # => 8 /cat/ =~ "catch" # => 0 /cat/ =~ "Cat" # => nil You can put the string first if you prefer:2 "dog and cat" =~ /cat/ # => 8 "catch" =~ /cat/ # => 0 "Cat" =~ /cat/ # => nil – 22 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 Regular Expressions Examples – 23 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 Using Match results in Control Flow str = "cat and dog" if str =~ /cat/ puts "There's a cat here somewhere" end • testing when a pattern does not match a string using !~: File.foreach("testfile").with_index do |line, index| puts "#{index}: #{line}" if line !~ /on/ end produces: 1: This is line two 2: This is line three – 24 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 Changing Strings with Patterns str = "Dog and Cat" new_str1 = str.sub(/a/, "*") new_str2 = str.gsub(/a/, "*") puts "Using sub: #{new_str1}" puts "Using gsub: #{new_str2}“ produces: Using sub: Dog *nd Cat Using gsub: Dog *nd C*t – 25 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 Grouping You can use parentheses to group terms within a regular expression. Everything within the group is treated as a single regular expression. # This matches an 'a' followed by one or more 'n's show_regexp('banana', /an+/) # => b->an<-ana # This matches the sequence 'an' one or more times show_regexp('banana', /(an)+/) # => b->anan<-a – 26 – CSCE 740 Spring 2014 Grouping to save portions that match /(\d\d):(\d\d)(..)/ =~ "12:50am" # => 0 "Hour is #$1, minute #$2" # => "Hour is 12, minute 50" /((\d\d):(\d\d))(..)/ =~ "12:50am" # => 0 "Time is #$1" # => "Time is 12:50" "Hour is #$2, minute #$3" # => "Hour is 12, minute 50" "AM/PM is #$4" # => "AM/PM is am" – 27 – CSCE 740 Spring 2014 show_regexp def show_regexp(string, pattern) match = pattern.match(string) if match "#{match.pre_match}->#{match[0]}<-#{match.post_match}" else "no match" end end – 28 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 show_regexp Examples show_regexp('very interesting', /t/) # => very in->t<-eresting show_regexp('Fats Waller', /a/) # => F->a<-ts Waller show_regexp('Fats Waller', /lle/) # => Fats Wa->lle<-r show_regexp('Fats Waller', /z/) # => no match – 29 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 Anchors Pattern ^ $ \A \z and \Z \Z description match the beginning of a line match the end of a line matches the beginning of a string Match the end of a string matches the end of a string unless the string ends with \n; it matches just before match word boundaries and nonword boundaries \b and \B – 30 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 Anchor Regular Expressions Examples str = "this is\nthe time" show_regexp(str, /^the/) # => this is\n->the<- time show_regexp(str, /is$/) # => this ->is<-\nthe time show_regexp(str, /\Athis/) # => ->this<- is\nthe time show_regexp(str, /\Athe/) # => no match show_regexp("this is\nthe time", /\bis/) # => this ->is<-\nthe time show_regexp("this is\nthe time", /\Bis/) # => th->is<- is\nthe time – 31 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 Character classes character class - [characters] matches any single character between the brackets For example [aeiou] [cat] = [tac] negated class - [^xyz] matches any single character not x, y or z show_regexp('Price $12.', /[aeiou]/) show_regexp('Price $12.', /[\s]/) show_regexp('Price $12.', /[$.]/) – 32 – # => Pr->i<-ce $12. # => Price-> <-$12. # => Price ->$<-12. http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 Character class Examples a = 'see [The PickAxe-page 123]' show_regexp(a, /[A-F]/) # => see [The Pick->A<-xe-page 123] show_regexp(a, /[A-Fa-f]/) # => s->e<-e [The PickAxe-page 123] show_regexp(a, /[0-9]/) # => see [The PickAxe-page ->1<-23] show_regexp(a, /[0-9][0-9]/) # => see [The PickAxe-page ->12<-3] – 33 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 Character class abbreviations Sequence As [ ... ] \d [0-9] ASCII decimal digit character \D [^0-9] Any character except a digit \h [0-9a-fA-F] Hexadecimal digit character \H [^0-9a-fA-F] Any character except a hex digit \s [ \t\r\n\f] ASCII whitespace character \S [^ \t\r\n\f] Any character except whitespace \w [A-Za-z0-9\_] ASCII word character \W [^A-Za-z0-9\_] Any character except a word character – 34 – Meaning http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 Ruby Regular Expr Changing text if line =~ /Perl|Python/ puts "Scripting language mentioned: #{line}” end line.sub(/Perl/, 'Ruby') # replace first 'Perl' with 'Ruby‘ line.gsub(/Python/, 'Ruby') # replace every 'Python' with 'Ruby' – 35 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 Regexp Constructor Regexps are created using the /.../ and %r{...} literals, “r” for raw don’t cook the ‘/’ and by the Regexp::new constructor. – 36 – CSCE 740 Spring 2014 the %r{...} syntax The %r syntax is particularly useful when creating patterns that contain forward slashes: %r{} is equivalent to the /.../ notation, but allows you to have '/' in your regexp without having to escape them: %r{/home/user} is equivalent to /\\/home\\/user/ /mm\/dd/ # => /mm\/dd/ Regexp.new("mm/dd") # => /mm\/dd/ %r{mm/dd} # => /mm\/dd/ – 37 – CSCE 740 Spring 2014 Regular Expressions Examples date = "12/25/2010" date =~ %r{(\d+)(/|:)(\d+)(/|:)(\d+)} [$1,$2,$3,$4,$5] # => ["12", "/", "25", "/", "2010"] date =~ %r{(\d+)(?:/|:)(\d+)(?:/|:)(\d+)} [$1,$2,$3] # => ["12", "25", "2010"] – 38 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 Backslash Sequences in Substitutions puts "fred:smith".sub(/(\w+):(\w+)/, '\2, \1') puts "nercpyitno".gsub(/(.)(.)/, '\2\1') produces: smith, fred encryption More use of backslashes \& (last match), \+ (last matched group), \‘ (string prior to match), \’ (string after match), and \\ (a literal backslash). – 39 – CSCE 740 Spring 2014 str = 'a\b\c' # => "a\b\c" str.gsub(/\\/, '\\\\\\\\') # => "a\\b\\c“ However, using the fact that \& is replaced by the matched string, you could also write this: str = 'a\b\c' # => "a\b\c" str.gsub(/\\/, '\&\&') # => "a\\b\\c" If you use the block form of gsub, the string for substitution is analyzed only once : str = 'a\b\c' # => "a\b\c" str.gsub(/\\/) { '\\\\' } # => "a\\b\\c" – 40 – CSCE 740 Spring 2014 Backslashes in Patterns same = "12:15-12:45" differ = "12:45-13:15“ # use numbered backreference same =~ /(\d\d):\d\d-\1:\d\d/ # => 0 differ =~ /(\d\d):\d\d-\1:\d\d/ # => nil – 41 – CSCE 740 Spring 2014 Cases /pat/i # i for ignore case /\.(gif|jpg|jpeg|png)$/i def mixed_case(name) name.downcase.gsub(/\b\w/) {|first| first.upcase } end mixed_case("DAVE THOMAS") # => "Dave Thomas" mixed_case("dave thomas") # => "Dave Thomas" mixed_case("dAvE tHoMas") # => "Dave Thomas" – 42 – CSCE 740 Spring 2014 Lookahead and Lookbehind – 43 – CSCE 740 Spring 2014 Rubular Rubular: a Ruby regular expression editor and tester [abc] A single character of: a, b or c [^abc] Any single character except: a, b, or c [a-z] Any single character in the range a-z [a-zA-Z] Any single character in the range a-z or A-Z ^ Start of line $ End of line \A Start of string \z End of string – 44 – http://rubular.com/ CSCE 740 Spring 2014 (?<month>\d{1,2})\/(?<day>\d{1,2})\/(?<year>\d{4}) Today's date is: 1/21/2014. – 45 – CSCE 740 Spring 2014 Regular Expression Options i Case insensitive. The pattern match will ignore the case of letters in the pattern and string. o Substitute once. Any #{...} substitutions in a particular regular expression literal will be performed just once, the first time it is evaluated. Otherwise, the substitutions will be performed every time the literal generates a Regexp object. m Multiline mode. Normally, “.” matches any character except a newline.With the /m option, “.” matches any character. x Extended mode. Complex regular expressions can be difficult to read. The x option allows you to insert spaces and newlines in the pattern to make it more readable. You can also use # to introduce comments. – 46 – CSCE 740 Spring 2014 BLOCKS – 47 – CSCE 740 Spring 2014 Blocks a = %w( ant bee cat dog elk ) # create an array a.each { |animal| puts animal } # iterate over the contents Yield – will be discussed next time [ 'cat', 'dog', 'horse' ].each do |animal| print animal, " -- " – 48 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 { puts "Hello" } do # this is a block # club.enroll(person) # and so is this person.socialize end – 49 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 Blocks 5.times { print "*" } 3.upto(6) {|i| print i } ('a'..'e').each {|char| print char } *****3456abcde – 50 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 def callBlock yield yield end callBlock { puts "In the block" } Produces In the block In the block – 51 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 Ruby I/O • Already seen • • • puts print P • On reading • • Gets reads line from stdin variable $_ Iterate over lines of file line = gets print line – 52 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 Processing stdin while gets if /Ruby/ print end end = ARGF # assigns line to $_ # matches against $_ # prints $_ Now the “ruby way” ARGF.each { |line| print line if line =~ /Ruby/ } – 53 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 Classes, Objects, and Variables class Song def initialize(name, artist, duration) @name = name @artist = artist @duration = duration end end aSong = Song.new("Bicylops", "Fleck", 260) – 54 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 aSong = Song.new("Bicylops", "Fleck", 260) aSong.inspect >> #<Song:0x401b4924 @duration=260, @artist=\"Fl eck\", @name=\"Bicylops\"> aSong.to_s – 55 – >> "#<Song:0x401b499c>” http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 New improved to_s class Song def to_s “Song: #{@name} -- #{ @artist } ( #{ @duration } )” end end aSong = Song.new("Bicylops", "Fleck", 260) aSong.to_s – 56 – >> “Song: Bicylops--Fleck (260)” http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 Inheritance class KaraokeSong < Song def initialize(name, artist, duration, lyrics) super(name, artist, duration) @lyrics = lyrics end End aSong = KaraokeSong.new("My Way", "Sinatra", 225, " And now, the...") aSong.to_s … – 57 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 overriding to_s class KarokeSong def to_s “KS: #{@name} -- #{ @artist } ( #{ @duration } ) [#{@lyrics}]” end end class KaraokeSong < Song def to_s super + " [#{@lyrics}]“ end end – 58 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 Accessing instance variables Class Song attr_reader :name, :artist, :duration attr_writer :duration … end – 59 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 class JavaSong { // Java code private Duration myDuration; public void setDuration(Duration newDuration) { myDuration = newDuration; } } class Song attr_writer :duration end aSong = Song.new("Bicylops", "Fleck", 260) aSong.duration = 257 – 60 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 class Song @@plays = 0 def initialize(name, artist, duration) @name = name @artist = artist @duration = duration @plays = 0 end def play @plays += 1 @@plays += 1 "song: #@plays plays. Total #@@plays plays." end – 61 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 Implementing a SongList Container append( aSong ) » list Append the given song to the list. deleteFirst() » aSong Remove the first song from the list, returning that song. deleteLast() » aSong Remove the last song from the list, returning that song. [ anIndex } » aSong Return the song identified by anIndex, which may be an integer index or a song title. – 62 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 SongList: Initializer & append # Initializer class SongList def initialize @songs = Array.new end end #append method class SongList def append(aSong) @songs.push(aSong) self end – 63 – http://ruby-doc.org/docs/ProgrammingRuby/ end CSCE 740 Spring 2014 SongList: class SongList def deleteFirst @songs.shift end def deleteLast @songs.pop end end – 64 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 SongList: [ ] method 1rst version class SongList def [ ](key) if key.kind_of?(Integer) @songs[key] else # ... end end end – 65 – CSCE 740 Spring 2014 Class Variables class Song @@plays = 0 def initialize(name, artist, duration) @name = name @artist = artist @duration = duration @plays = 0 end def play @plays += 1 @@plays += 1 "This song: #@plays plays. Total #@@plays plays. end end – 66 – CSCE 740 Spring 2014 Class Methods class Example def instMeth # instance method … end def Example.classMeth # class method … end end – 67 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 Singletons class Logger private_class_method :new @@logger = nil def Logger.create @@logger = new unless @@logger @@logger end end Logger.create.id Logger.create.id – 68 – » » 537766930 537766930 http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 Access Control “Public methods can be called by anyone---there is no access control. Methods are public by default (except for initialize, which is always private). Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. Private methods cannot be called with an explicit receiver. Because you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendents within that same object.” – 69 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014 Specifying Access class MyClass def method1 # default is 'public' #... end protected # subsequent methods will be 'protected' def method2 # will be 'protected' #... end private # subsequent methods will be 'private' def method3 # will be 'private' #... end public # subsequent methods will be 'public' def method4 # and this will be 'public' #... end end http://ruby-doc.org/docs/ProgrammingRuby/ – 70 – CSCE 740 Spring 2014 Ruby 1.9 “Buy the book page” http://pragprog.com/book/ruby3/programming-ruby-1-9 • Regular Expressions (download pdf) • Namespaces, Source Files, and Distribution (download pdf) • Built-in Classes and Modules (download pdf of the entry for class Array) • Free Content … More on reg expr • http://www.ruby-doc.org/core-2.1.0/Regexp.html – 71 – http://ruby-doc.org/docs/ProgrammingRuby/ CSCE 740 Spring 2014