RegExes in Ruby — A Brief Summary

Similar to a hash, we can assign names to the things we’ve matched.

For this, we use the syntax /(?<key>regex)/ or /(?’key’regex)/.

Ruby refers to these as names within capture groups, but it might be easier to think of them as key/value pairs.

Here, “example” is of the MatchData class.

In the example above, regex[:name] will be assigned to the first instance of a capital letter [A-Z] followed by 2 or more word characters w, and regex[“age”] will be set to the first digit or consecutive digits.

Then, we can get data from the MatchData instance “example” like a hash (and like an array, as previously mentioned).

Lastly, we can get some interesting information from the same MatchData using the $.

These combinations with $ can give you the everything before the first match ($`), everything including and between the matches ($&), everything after the last match ($’), the first match ($1), and the second match ($2).

How else can regexes be useful?Checking email formats/.

+@.

+.

+/ matches any character 1 or more times, followed by @, followed by any character 1 or more times, followed by a period, followed by any character 1 or more timesEnsuring names are typed correctly/([A-Z].

+){2,}/ matches any capital character followed by 1 or more of any character, and all of that 2 or more timesChecking price data/$(?<dollars>d+).

(?<cents>d+)/ first matches any digits after the $ sign and assigns them to [:dollars], and then matches any digits after the period and assigns them to [:cents]Making password regulations/^(?=.

*[A-Z])(?=.

*[a-z])(?=.

*[0–9]).

{6,12}$/ between the beginning (^) and the end ($), there must be a capital letter, a lower case letter, and a digit.

All of these can come in any order (?=), and the whole thing must be 6 to 12 characters longSearching in Atom and other text editorsi*’*m* — with “case insensitive” on, Atom would find any instance of 0-∞ i or I, 0-∞ ‘, and 0-∞ m or M.

Keep in mind, this would also pick up any words with i, I, ‘, m, or M in them, but it would at least help you find a very badly mistyped “I’m.

”Actually, we can put this “case insensitivity” into our regexes outside of Atom, as well.

Options come after the regex’s closing and modify it.

The most common ones are/Ruby/iignores case, so ruby, Ruby, and RuBy all match.

/Ruby/xignores whitespace (s)/Ruby/mmatches multiple lines, and [enter] is just a character (.)There are tons of cool things you can do with regexes — like even checking that all of the letters in a string are from the Latin alphabet (/^p{Latin}+$/).

There are so many things you can do that many books have been written solely dedicated to regular expressions.

Hopefully this short summary has helped us become comfortable enough with the idea to understand some common regex patterns in code.

What can regexes help you do?Potential ProblemsSometimes we see /[A-Za-z0–9]/, which means any character in A-Z, a-z, or 0–9.

This is because computer languages typically use the UTF-8 encoding to evaluate individual characters, where 0 is 0030, 9 is 0039, A is 0041, Z is 005a, a is 0061, and z is 007a.

The regex /[0-z]/ would include all of those letters AND :, ;, <, =, >, ?, @ — the letters between 9 and A — AND [, , ], ^, _, and ` — the letters between Z and a.

Also, putting symbols in the wrong order throws a syntax error, because /[z-0]/ is equivalent to the range 007a to 0030, counting backwards.

As in everyday speech and standard writing, ranges are defined like [1–9] or “one through nine,” and not [9–1] or “nine through one.

”ResourcesClass: Regexp (Ruby 2.

6.

2)Class : Regexp – Ruby 2.

6.

2ruby-doc.

orgRuby Regular ExpressionsRuby Regular Expressions – Learn Ruby in simple and easy steps starting from basic to advanced concepts with examples…www.

tutorialspoint.

comWhat is the correct pronunciation of “regex”?English Language & Usage Stack Exchange is a question and answer site for linguists, etymologists, and serious English…english.

stackexchange.

comRegExr: Learn, Build, & Test RegExRegular expression tester with syntax highlighting, PHP / PCRE & JS Support, contextual help, cheat sheet, reference…regexr.

comRubularRuby-based regular expression editor/testerrubular.

com.. More details

Leave a Reply