Using Regular Expressions to Pack-a-Punch Ruby String Manipulation

Using Regular Expressions to Pack-a-Punch Ruby String ManipulationRomy MaghsoudiBlockedUnblockFollowFollowingMay 21what you see after using regular expressions in your codeA s someone who’s relatively new to programming and a total newbie with Ruby, I found something bewildering page after page of documentation.

A weird jumble of characters in the arguments of these functions that felt like an alien language.

source: https://www.

rubyguides.

com/2015/06/ruby-regex/This completely freaked me out and I had zero idea of what any of this meant.

It turns out these jumbles of characters are called regular expressions, or more commonly known as regexes, and they are a powerful tool when dealing with strings in Ruby.

Regexes are patterns used to match against strings and I didn’t even know what they were called at first, I just thought it was something beyond me that I’ll learn eventually once I’m a total Ruby pro, but that was a lie.

I knew they were powerful, yet I didn’t know how to use them.

I was trying to optimize some of my code and I knew that a Regex would allow me to do that, but it was still just hieroglyphics to me.

After an hour of struggling and trying different jumbles of characters, I found a gift from God, though he goes by Michael Lovitt, that would help me learn about the previously forsaken arguments.

“Rubular.

com” is an editor for Ruby regular expressions that has a little reference guide along with 3 boxes, 1 for your Regex, 1 for the string it’ll be used on, and the other for showing the results (what was selected by the regex).

Regex syntax looks intimidating, especially for newcomers, but with Rubular, you can write and implement a regex to help solve your problem.

source: https://rubular.

comRuby regexes are defined between 2 forward slashes and can be used as arguments to many Ruby methods as well as matching a regex against a string with the =~ operator, which would return the offset of the match or nil.

Their power shines through with enumerators such as select and map by allowing for more concise code and the convenience of typing just a little bit less.

That email.

match we saw earlier validates an email address by selecting the elements that an email should have then turns it into a boolean value using the !!, though in Ruby 2.

4 and later you can simply use email.

match?.

Using regexes to deal with larger strings such as log files is another one of its handy uses.

Here’s an example of parsing a log file:source: https://www.

rubyguides.

com/2015/06/ruby-regex/It may seem like magic at first, but we can walk through the LOG_FORMAT variable using the quick reference on the Rubular page.

“d{2}” refers exactly 2 digits and because it’s within the parenthesis it’ll capture everything inside, so 2 digits, a colon, and another 2 digits.

“w+” refers to one or more word characters.

Finally, “.

*” refers to zero or more single characters.

Now it doesn’t look as daunting and foreign as it did before.

Though something like the picture below would need a deeper dive into the documentation and Rubular’s quick reference wouldn’t be as insightful.

source: https://ruby-doc.

org/core-2.

4.

0/Regexp.

htmlRegExp is a Ruby class and can be a language within itself because of how long the documentation page is.

Regexes can use options that control how the pattern can match such as “/pat/i” which is an ignore case, can group in several different ways such as atomic grouping that uses “?>pat”, this causes pat to be treated as a non-divisible whole, as well as several other capabilities that enable you to be creative with your pattern matching.

Though it may seem intimidating at first, regular expressions are a very powerful tool when dealing with strings in Ruby.

To be able to write complex regexes and truly understand how the magic works, I’d suggest going over the documentation.

Rubular should help with most of the basic functionality and you could piece together things on the site, though you might not completely understand what you’re doing or how exactly it works.

Add regexes to your Ruby arsenal and you’ll be basically unstoppable, not really but still.

.

. More details

Leave a Reply