Skip to content

Commit

Permalink
few regexp examples added
Browse files Browse the repository at this point in the history
  • Loading branch information
Karthikeyan A K committed Aug 10, 2012
1 parent a4556e3 commit 3c5e719
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 3 deletions.
7 changes: 7 additions & 0 deletions regexp_capture.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/ruby
# regexp_capture.rb

print "Enter Birthday (YYYY-DD-MM) :"
date = gets.chop
/\d{4}-(\d{2})-\d{2}/.match date
puts "You were born on month: #{$1}"
7 changes: 7 additions & 0 deletions regexp_capture_1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/ruby
# regexp_capture_1.rb

print "Enter Birthday (YYYY-DD-MM) :"
date = gets.chop
/(\d{4})-(\d{2})-(\d{2})/.match date
puts "Year: #{$1} \n Month: #{$2} \n Date: #{$3}"
7 changes: 7 additions & 0 deletions regexp_capture_2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/ruby
# regexp_capture_2.rb

print "Enter Birthday (YYYY-DD-MM) :"
date = gets.chop
/\s*(\d{4})\s*-\s*(\d{2})\s*-\s*(\d{2})\s*/.match date
puts "Year: #{$1} \n Month: #{$2} \n Date: #{$3}"
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/ruby
# regexp_character_classes.rb
# regexp_character_classes_1.rb

string = gets.chop
puts "The string contains character(s) from a to z" if /[a-z]/.match string
Expand Down
4 changes: 2 additions & 2 deletions regexp_scan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
puts "The words are:"
p words
puts # prints a empty line
puts "there are #{words.count} in the string"
puts
puts "there are #{words.count} words in the string"
puts "there are #{words.uniq.count} unique words in string"
9 changes: 9 additions & 0 deletions regexp_scan_twitter_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/ruby
# regexp_scan_twitter_users.rb

string = """ There is a person @karthik_ak who wrote ilr. Its about a
language called @ruby invented by @yukihiro_matz """

users = string.scan(/@\w+/)
puts "The users are:"
p users

0 comments on commit 3c5e719

Please sign in to comment.