Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8754677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:40:51+00:00 2026-06-13T13:40:51+00:00

I need to store a regular expression related to other fields in a database

  • 0

I need to store a regular expression related to other fields in a database table with ActiveRecord.

I found the to_s method in Regexp class which states

Returns a string containing the regular expression and its options
(using the (?opts:source) notation. This string can be fed back in to
Regexp::new to a regular expression with the same semantics as the
original. (However, Regexp#== may not return true when comparing the
two, as the source of the regular expression itself may differ, as the
example shows). Regexp#inspect produces a generally more readable
version of rxp.

So it seems a working solution, but it will store the exp with an unusual syntax and in order to get the string to store I need to build it manually with /my-exp/.to_s. Also I may not be able to edit to regexp directly. For instance a simple regexp produces:

/foo/i.to_s # => "(?i-mx:foo)" 

The other option is to eval the field content so I might store the plain expression in the db column and then doing an eval(record.pattern) to get the actual regexp. This is working and since I’m the only one who will be responsible to manage the regexp records there should be no issues in doing that, except application bugs 😉

Do I have other options? I’d prefer to not doing eval on db fields but on the other side I don’t want to work with a syntax which I don’t know.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:40:52+00:00Added an answer on June 13, 2026 at 1:40 pm

    Not sure I understand your constraints exactly.

    If you store a string in db, you could make a Regexp from it:

    a = 'foo'
    => "foo" 
    /#{a}/
    => /foo/
    Regexp.new('dog', Regexp::EXTENDED + Regexp::MULTILINE + Regexp::IGNORECASE) 
    => /dog/mix
    

    There are other constructors, see doc.

    The very best solution to not use eval’d code is to store the regexp part in a string column and flags in a separate integer column. In this way the regexp can be built with:

    record = Record.new pattern: 'foo', flags: Regexp::IGNORECASE
    Regexp.new record.pattern, record.flags # => /foo/i
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working with regular javascript, I need to store info into an array for output
I need to store a very large amount of instances of my class, and
I need to store GPS coordinates in a database. I've heard that floats are
I need to store lotto numbers in a SQL database. There are 8 separate
I need to perform a regular expression search for a string x in another
Possible Duplicate: Using regular expression within a stored procedure I need to validate a
I need a regular expression that matches APA format references. I currently have this:
I need store just 10 arrays in my app, which I can change from
I have id values for products that I need store. Right now they are
I need to store a login session when the user is login and remove

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.