Ruby: You can nest regular expressions
Ruby lets you re-use existing RegExp
objects by interpolating it into new patterns:
locales_pattern = /de|en|fr|es/i
html_tag_pattern = /<html lang="#{locales_pattern}">/
Any modifiers like /i
or /x
will be preserved within the interpolated region, which is pretty cool. So in the example above only the interpolated locales are case-insensitive, while the pattern around it (/<html .../
) remains case-sensitive.
Related cards:
Ruby: Making your regular expressions more readable with /x and alternative delimiters
The following two hints are taken from Github's Ruby style guide:
If your regular expression mentions a lot of forward slashes, you can use the alternative delimiters %r(...)
, %r[...]
or %r{...}
instead...
How can you detect if two regular expressions overlap in the strings they can match? - Stack Overflow
I have a container of regular expressions. I'd like to analyze them to determine if it's possible to generate a string that matches more than 1 of them. Short of writing my own regex engine with this use case in mind, is there an easy way in C++ o...
Use look-behind assertions in regular expressions with Ruby 1.8
Regular expressions can have something called "zero-width look-behind assertions". This means that you want a pattern to be preceded by another pattern, but not include the preceding pattern in your match or search cursor. E.g. (?<=x)y
matches `...
Regular Expressions: Excessive backtracking can get yourself in trouble
Two weeks ago, Cloudflare was struck by a global outage that lasted ~30 minutes. The incident was rooted on a CPU exhaustion caused by a single regular expression containing some [catastrophic backtracking](https://www.regular-expressions.info...
RubyMine: You can disable inspections you don't care about
When you find yourself constantly ignoring a RubyMine warning, you can simple disable that warning and de-clutter your editor. E.g. in my Cucumber scenarios RubyMine underlines 90% of all lines because it does not know about [spreewald](https://gi...
Ruby: String representations of regular expressions
Ruby's regular expressions can be represented differently.
When serializing them, you probably want to use inspect
instead of to_s
.
For the examples below, consider the following Regexp
object.
regexp = /^f(o+)!/mi
to_s
Using ...
Ruby's percent notation can do more than strings
Percent Notation
We already know that that we can create strings using the percent notation:
%(<foo="bar's ton">)
is perfectly fine Ruby.
Modifier
Bu...
Regular Expressions - Cheat Sheet
You can write regular expressions some different ways, e.g. /regex/
and %r{regex}
. For examples, look here.
Remember that it is always a good idea [to matc...
The Exciter - Why You Should Deploy Your Next Application on Ruby 1.9 and a Rant in General
The rubyforge gems model may not be perfect, but damnit people, when there’s a gem update I know that it has actually been tested somewhat and it’s not just whatever random point HEAD happens to be at, at that point in time, by some random Joe who...
Fix [RubyODBC]Cannot allocate SQLHENV when connecting to MSSQL 2005 with Ruby 1.8.7. on Ubuntu 10.10
I followed this nice guide Connecting to MSSQL with Ruby on Ubuntu - lambie.org until I ran in the following errors:
irb(main):001:0> require "dbi"; ...