The conversation went something like this:
Instructor: “Here’s a hint for today’s group lab: use Regular Expessions.”
Me, external: “Got it!”
Me, internal: What the #^$(&#& is a ‘Regular Expression?’
So I head to the internet:
Regular expressions (regexps) are patterns which describe the contents of a string. They’re used for testing whether a string contains a given pattern, or extracting the portions that match. They are created with the
/
pat/
and%r{
pat}
literals or theRegexp.new
constructor.A regexp is usually delimited with forward slashes (
/
).
I’ve long worked software developer-adjacent, and I’ve dabbled in the command line. While the regular English phrase “Regular Expression” was not immediately available in my memory banks, Regex is. Regex = Regular Expression. Always Be Learning.
The lab that day was called Piglatinizer. Idea was to take in a string, one word or many, and convert it to pig latin.1 . Pig latin constructed by splitting a word by the first vowel, appending the first part to then end of the second and applying “ay.” Words that start with a vowel simply have “way” (or other, regional variations) stuck to the end. So “snickers” becomes “ickerssnay” and “apple” becomes “appleway.”
SO MUCH FUN, RIGHT? 2
My partner and I started y breaking the string into an array using the method .split(“”) and then iterating over the array until we found the vowel. So we tried something like this:
arr = string.split("")
pig = []
latin =[]
arr.each do |a|
while !a.match?(/[aeiou]/)
pig << a
arr.shift
else
latin << arr
end
end
Which and of course that didn’t work at all. We tried until and while loops, but couldn’t get there. Eventually, the answer was shared and we all acknowledged our deficiencies and we left for the day to tackle more labs and learn about Ruby on Rails.
But I kept thinking about regex and what exactly /[aeiou]/ did when paired with the match method, and how it can help me with other methods. I know that regex is used to validate phone numbers, credit card numbers, and other values that follow a specific pattern, but there’s likely more.
Basic Syntax
Here are our vowels as a regular expression:
/aeiou/
In Ruby and in other programming languages, regular expressions are contained with in the forward slash (/). This is to not be confused with the back slash (\) which is used to escape non-word character such as $.&*#, etc.
/[aeiou]/
When an expression is placed within the [], this indicates the Character class. A match is made to any one value (a or e) rather than in the previous example (a and e together). In our code sample above, string.match?(/[aeiou]/), my code is (attempting to) match the first vowel in the string and return an object with that vowel. In the string “snickers,” the object returned would be “i”
Matching Operator
In much of the documentation I found on regex, examples and tests were shown with the matching operator:
=~ #matching operator
When the matching operator is used, the index number of the first match is returned by Ruby. If there are no matches, Nil is returned. This can be set to a variable.
"snickers" =~ /[aeiou]/
=> 2
I will also note that regular expressions are, by default, case sensitive. So:
"SNICKERS" =~ /[aeiou]/
=> nil
To correct for that, you can add an “i” after the regular expression.
"SNICKERS =~ /[aeiou]/i
=> 2
Methods
The matching operator is not the choice I am familiar with, however, in code I’ve written and I’ve seen. Again, in my code above and in other examples, I’ve used match, match?, or split and a regular expression (not clearly known at the time) to break up a string or consider a boolean for proceeding with in an action. There are many others.
.match returns an object, MatchData, that you can use for a number of things, including, as I will, parse a string at the first vowel
string = snickerdoodle
string.match([/aeiou/])
=> <MatchData "i">
.match? returns a value of either true, the string matches the regular expression, or false, it does not.
string = snickerdoodle
string.match?(/[aeiou]/)
=> true
.split breaks up the string at every character that matches the regex as part of an array.
string = snickerdoodle
string.split(/[aeiou]/)
=> ["sn", "ck", "rd", "", "dl"]
And if you pass a parameter, say index 0, you can return just the item in the array you want
string = snickerdoodle
string.split(/[aeiou]/)[0]
=> "sn"
Piglatinizer
Shamefully, it took me more than a few tries to get this the Piglatinizer to work. Although the lab called for a Rails UI, I built this here in the command line, via repl.it.
puts " ********** Piglatinizer ********** "
puts ""
puts "Please enter the word or sentence that you would like to have converted to pig latin!"
puts ""
string = gets.chomp.downcase
puts ""
def piglatinizer(string)
p = string.split(" ")
pig_array = []
p.each do |word|
pig = piglatin(word)
pig_array << pig
end
return pig_array.join(" ").capitalize
end
def piglatin(word)
if word[0].match?(/[aeiou]/i)
piglatin = word + "way"
else
i = word.index(/[aeiou]/i)
l = word.length
latin = word3
pig = word[i..l]
piglatin = pig + latin + "ay"
end
return piglatin
end
puts piglatinizer(string)
"***End of Line***"
Feel free to take a look at it and run it here.
Resources
Here are some of the resources that I hit while doing my research. I expect that I’ll turn back to them as I get neck deep in Ruby, Rails, and Javascript, and as I consider my final project.
I am also building out a one-page regex resource and will add to it as my experience grows. There are some in the below, usually buried within the page.
- http://zetcode.com/lang/rubytutorial/regex/
- http://rubylearning.com/satishtalim/ruby_regular_expressions.html
- https://www.rexegg.com/regex-quickstart.html
- https://ruby-doc.org/core-2.4.0/Regexp.html
- https://regex101.com or https://rubular.com/
- My repl https://repl.it/@bluthgeld/PigLatinizerTIWYGWYMWU
- A brief story: when I was in 4th grade we lived in Southern California. My friends and I had learned about pig latin and were trying it out on the playground. The recess monitor, a woman likely in her 70s, told us about a gibberish language she used in her childhood where she swapped out the middle of words and replaced them with “apple.” That old woman was Steve Jobs great-grandmother. Falsey story. ↩
- ↩
- .i – 1 ↩