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++ or Python to solve this problem?
Related cards:
JavaScript: How to generate a regular expression from a string
Getting a regular expression from a string in JavaScript is quite simple:
new RegExp('Hello Universe');
# => /Hello Universe/
You can also use special characters:
new RegExp('^(\\d+) users')
# => /^(\d+) users/
Our expressi...
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...
jQuery: How to remove classes from elements using a regular expression
jQuery's removeClass
removes the given class string from an element collection. If you want to remove multiple/unknown classes matching a given pattern, you can do that.
For example, consider a DOM node for the following HTML. We'll reference i...
RSpec matcher to check if two numbers are the same
You can usually just use the eq
matched to compare two numbers:
expect(deal.total).to eq(120)
If the actual value is a BigDecimal
, you might have issues when you match it against a Float
:
expect(deal.total_price).to eq(1200.99...
RSpec: how to prevent the Rails debug page if you want to actually test for 404s
Within development and test environments, Rails is usually configured to show a detailed debug page instead of 404s. However, there might be some cases where you expect a 404 and want to test for it.
An example would be request-specs that ch...
What collapsing margins are, how they work and when margins do not collapse
Imagine you have 2 HTML boxes. The first one has a margin-bottom
of let's say 30px
and the second one a margin-top
of 20px
. After rules of collapsing margins have been applied we ha...
Check if two arrays contain the same elements in Ruby, RSpec or Test::Unit
RSpec 1, RSpec 2
To test whether two arrays have the same elements regardless of order, RSpec 1 and 2 give you the =~
matcher:
actual_array.should =~ expected_array
Rspec 3
With RSpec 3's expect
s...
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 ...
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...
RSpec: How to check if a string contains terms in a desired order
There seems to be no built-in matcher in RSpec to check if a string contains terms in the desired order. A simple workaround is to use a regular expression that also matches newlines (m
-modifier).
Cons:
- The readability when terms need to be...