I am looking for a database that can store regex expressions as values. E.g. somthing like this:
{:name => \"Tim\", :count => 3, :expression => /t+/
MongoDB will allow you to store actual regular expressions (i.e. not a string representing a regular expression), as shown below:
> db.mycoll.insertOne({myregex: /aa/})
{
"acknowledged" : true,
"insertedId" : ObjectId("5826414249bf0898c1059b38")
}
> db.mycoll.insertOne({myregex: /a+/})
{
"acknowledged" : true,
"insertedId" : ObjectId("5826414949bf0898c1059b39")
}
> db.mycoll.find()
{ "_id" : ObjectId("5826414249bf0898c1059b38"), "myregex" : /aa/ }
{ "_id" : ObjectId("5826414949bf0898c1059b39"), "myregex" : /a+/ }
You can use this to then query for rows with a regex that matches a query, as follows:
> db.mycoll.find(function() { return this.myregex.test('a'); } )
{ "_id" : ObjectId("5826414949bf0898c1059b39"), "myregex" : /a+/ }
Here we search for rows where the string 'a' is matched by the myregex
field, resulting in the second document, with regex /a+/
, being returned.