I am not a Regex master by any means, but I do get a little gleeful when I see it. Sometimes it’s downright fun. If you want to see what I mean, try these Regex Crossword puzzles.
Regex’s matching capability is well known. For instance, we can search for patterns within strings.
gsub, replacing any pattern is easy peasy.
But what if you want to insert instead of replace? Say we have a variable that we want to convert from camel case to underscore format. Let’s see how to go from
Looking for capital letters makes sense. This first step isn’t necessary, but it demonstrates how to capture a Regex match for use later on. To create a capture, wrap the desired pattern in parenthesis:
See how the result has saved the letter S to position 1? Now we can recall that captured value as many times as needed by referring to its capture position. This is called a backreference. We can put the backreference in the replacement portion of a gsub, along with our underscore:
But what if we don’t know how many humps our camel has? Because each capture can only be tied to one match, we are extremely reliant on the string’s format when using captures and backreferences. We’ll need to capture more information to make sure we insert the underscores in the right position. Notice how multiple captures result in multiple backreferences:
Using these backreferences, this is how it looks when we add in our underscores:
That got ugly quickly, and it’s still not very dynamic. Using the string method
scan, we have the flexibility to deal with any crazy incoming camel case string you can think of without having to keep track of captures and backreferences. Now we can change as many matched instances as the string dictates:
This last example can be done in other ways, too. We could make use of a capture within the block. Or we could use
each_with_index to examine individual characters in relation to the characters around them. Let me know your suggestions!