Word boundaries in MySQL regular expressions
In regular expressions you can use the zero-width pattern \b
to match the beginning or end of a word:
note.title =~ /\bfoo\b/
Unfortunately \b
is not available in MySQL. You can use [[:<:]]
and [[:>:]]
to match the beginning and end of a word instead:
SELECT * FROM notes WHERE title REGEXP "[[:<:]]foo[[:>:]]"
These markers are unique to MySQL and not available in Ruby regular expressions.
Related cards:
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...
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: Space Separators
Matching the "space" character class
For matching whitespaces in a regular expression, the most common and best-known shorthand expression is probably \s
.
It matches the following whitespace characters:
- " " (space)
- \n (newline)
- \r (...
Regular Expressions: Quantifier modes
When you repeat a subpattern with a *
, +
or {...}
operator, you may choose between greedy, lazy and possessive modes.
Switching modes may affect the result and performance of your regular expressions. In the worst case, an ill-s...
Linux: Using grep with a regular expression
You can use three different versions of the regular expression syntax in grep
:
- basic:
-G
- extended:
-E
(POSIX) - perl:
-P
(PCRE)
[Difference between basic and extended](https://www.gnu.org/software/grep/manual/html_node/Basic-vs-Extend...
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...
Git: How to add changes matching a regular expression
When you have many changes, and you want to spread them across different commits, here is a way to stage all changes matching a given regular expression for a single commit.
Example
Consider the following git diff
output.
diff --gi...
Matching line feeds with regular expressions works differently in every language
Although regular expression syntax is 99% interchangeable between languages, keep this in mind:
- By default, the dot character (
"."
) does not match a line feed (newline, line break,"\n"
) in any language. - Some languages allow you to mo...
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...
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 `...