Is there a database that can store regex as values?

后端 未结 2 1972
野趣味
野趣味 2021-01-18 01:05

I am looking for a database that can store regex expressions as values. E.g. somthing like this:

{:name => \"Tim\", :count => 3, :expression => /t+/         


        
2条回答
  •  无人共我
    2021-01-18 01:56

    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.

提交回复
热议问题