Using Regular Expressions


Today I was given a coding challenge by DBC to convert a sentence into pig latin. The rules for pig latin are that any words that start with a vowel are left alone, but words that start with a constant are changed. The change is removing all the constants ahead of the first vowel in the word and placing them at the end of the word, with an -ay attached at the very end.

Ex: hello world => ello-hay orld-way

When I first attempted to solve this problem, I had around 8-10 lines of code. But once I learned RegExp, I was able to solve it in one line. Below is the code with RegExp.

def pig_latin

puts gets.chomp.split(" ").map {|word| word.gsub(/(\A[^aeiou]+)(.+)/,'\2-\1ay')}.join(" ")



So what is happening? lets start with the method gsub. gsub is a String method that takes a parameter to match and potentially replace characters in a string. The basic syntax of gsub is as follows: gsub(thing to match,thing to replace)

RegExp defines patterns, so by combining Regexp with gsub, I can manipulate characters to follow pig-latin's rules.The syntax for RegExp within the parameter is as follows:

/RegExp/,'to display'

RegExp also has something called capture groups which allow you to manipulate groups of characters by encapsulating the corresponding RegExp within parens, (). You can use multiple parens to manipulate multiple groups, and these groups are assigned autoincrementally. So for our previous example of how I solved pig-latin, we have two distinct capture groups: (\A[^aeiou]+) and (.+). After the , within '' the capture groups are assigned as \1 and \2 respectively. So you can manuver the order of these characters and whether or not a group of characters is displayed.


Rubular Youtube:RegExp Demystified