Diving deeper into the world of Regular Expressions in Ruby

We can approve // is a regex by calling the class method on it
You can see both outputs from lines 3 and 4 are the same
The output returns all the the words found within our phrase
We load in our info.txt file and output it to the screen
  1. Pimento, Jane, Celestial, Mr, then follow by an optional dot, then a space
  2. Thornwolf, J, James, Parsley, follow by an optional dot or comma, then a space
  3. Sr, Wildfire, Heathen, Densilated
The i after our / / regex is the option to ignore case
  1. We know the the street number always begins with a digit, optionally there could be a dash, and one or more digits, a space.
  2. The names of the addresses start with one or more letters, then a space, then one or more occurrences of a word character (Den, of, Dr, 99).
  3. In the case of Hall of Nomads, we can use a wildcard plus one or more occurrences to close it off. In all other cases, the first part of the addresses end at the second step above already.
  4. After a newline, the City name starts with a letter, and afterwards we can use the same technique to finish the State and zip code off with a wildcard.
  1. At the start of the line, there’s zero or one digit, follow by zero or one dash and left parenthesis.
  2. We have three consecutive numbers in all four cases.
  3. There could be one dash, a right parenthesis and space.
  4. In the first three cases, the middle parts are all numbers. And in the last case, we have letters. To denote one or more word characters(letter, digit, or underscore), we can use \w+
  5. In the first two and last case, we have a dash. And in the third case, we don’t have any. Then One or more word characters at the end.
We are able to grab all four phone numbers

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store