You are previewing Eloquent Ruby.

Eloquent Ruby

Cover of Eloquent Ruby by Russ Olsen Published by Addison-Wesley Professional
  1. Title Page
  2. Copyright Page
  3. Praise for Eloquent Ruby
  4. Contents
  5. Foreword
  6. Preface
  7. Acknowledgments
  8. About the Author
  9. Part I. The Basics
    1. Chapter 1. Write Code That Looks Like Ruby
    2. Chapter 2. Choose the Right Control Structure
    3. Chapter 3. Take Advantage of Ruby’s Smart Collections
    4. Chapter 4. Take Advantage of Ruby’s Smart Strings
    5. Chapter 5. Find the Right String with Regular Expressions
    6. Chapter 6. Use Symbols to Stand for Something
    7. Chapter 7. Treat Everything Like an Object—Because Everything Is
    8. Chapter 8. Embrace Dynamic Typing
    9. Chapter 9. Write Specs!
  10. Part II. Classes, Modules, and Blocks
    1. Chapter 10. Construct Your Classes from Short, Focused Methods
    2. Chapter 11. Define Operators Respectfully
    3. Chapter 12. Create Classes That Understand Equality
    4. Chapter 13. Get the Behavior You Need with Singleton and Class Methods
    5. Chapter 14. Use Class Instance Variables
    6. Chapter 15. Use Modules as Name Spaces
    7. Chapter 16. Use Modules as Mixins
    8. Chapter 17. Use Blocks to Iterate
    9. Chapter 18. Execute Around with a Block
    10. Chapter 19. Save Blocks to Execute Later
  11. Part III. Metaprogramming
    1. Chapter 20. Use Hooks to Keep Your Program Informed
    2. Chapter 21. Use method_missing for Flexible Error Handling
    3. Chapter 22. Use method_missing for Delegation
    4. Chapter 23. Use method_missing to Build Flexible APIs
    5. Chapter 24. Update Existing Classes with Monkey Patching
    6. Chapter 25. Create Self-Modifying Classes
    7. Chapter 26. Create Classes That Modify Their Subclasses
  12. Part IV. Pulling It All Together
    1. Chapter 27. Invent Internal DSLs
    2. Chapter 28. Build External DSLs for Flexible Syntax
    3. Chapter 29. Package Your Programs as Gems
    4. Chapter 30. Know Your Ruby Implementation
    5. Chapter 31. Keep an Open Mind to Go with Those Open Classes
  13. Appendix. Going Further
  14. Index
  15. Footnotes
    1. Preface
    2. Acknowledgments
    3. Chapter 1
    4. Chapter 2
    5. Chapter 3
    6. Chapter 4
    7. Chapter 5
    8. Chapter 6
    9. Chapter 7
    10. Chapter 8
    11. Chapter 9
    12. Chapter 10
    13. Chapter 11
    14. Chapter 12
    15. Chapter 13
    16. Chapter 14
    17. Chapter 15
    18. Chapter 16
    19. Chapter 17
    20. Chapter 18
    21. Chapter 19
    22. Chapter 20
    23. Chapter 21
    24. Chapter 22
    25. Chapter 23
    26. Chapter 24
    27. Chapter 25
    28. Chapter 26
    29. Chapter 27
    30. Chapter 28
    31. Chapter 29
    32. Chapter 30
    33. Chapter 31
O'Reilly logo

Chapter 5. Find the Right String with Regular Expressions

I’d like to pose a problem for you: Imagine that you have some text stored in a string, and somewhere in that text hides a time, something like 09:24 AM. You need to write a program that will locate the time. How do you do it? You might, using the methods we talked about in the last chapter, scan the string for a digit and then check to see whether it is followed by another digit and then see whether that is followed by a colon, then two more digits . . . well, you get the picture. Doing all this with the String methods is certainly possible. Tedious, but possible.

Sometimes a problem speaks to you. This time-finding problem is trying to tell you that you are using the wrong tool. The ...

The best content for your career. Discover unlimited learning on demand for around $1/day.