openlang

Ruby regex fails when constructed from a stored string

I am keeping a regular expression in the following variable - pattern_text = "/\A([^@\s]+)@((?:c-d+\.)+(in|com))\z/".

"mailbox@c-d.in".match?(/\A([^@\s]+)@((?:c-d+\.)+(in|com))\z/) => returns true
Regexp.new(pattern_text) => returns /\/A([^@ ]+)@((?:c-d+.)+(in|com))z\//
"mailbox@c-d.in".match?(Regexp.new(pattern_text)) => returns false

Saving this regular expression to my database automatically strips the \. The regex validator that I receive arrives as a string. Why doesn't that version work?

The conversation continues 2

Follow the reasoning
  1. Permalink

    Choose single-quoted strings rather than double-quoted ones (see this reference (https://en.wikibooks.org/wiki/Ruby_Programming/Strings#Single_quotes))

    > puts "/\A([^@\s]+)@((?:c-d+\.)+(in|com))\z/"
    /A([^@ ]+)@((?:c-d+.)+(in|com))z/
    
    > puts '/\A([^@\s]+)@((?:c-d+\.)+(in|com))\z/'
    /\A([^@\s]+)@((?:c-d+\.)+(in|com))\z/

    After that, discard the '/' at either end:

    > pattern_text = '\A([^@\s]+)@((?:c-d+\.)+(in|com))\z'
    => "\\A([^@\\s]+)@((?:c-d+\\.)+(in|com))\\z"
    
    > compiled = Regexp.new(pattern_text)
    => /\A([^@\s]+)@((?:c-d+\.)+(in|com))\z/
    
    > "mailbox@c-d.in".match?(Regexp.new(compiled))
    => true
  2. Permalink

    When putting a regular expression pattern into a database for later use, it is customary to save a string representation of the pattern / flags.

    So, when all regex information belongs in one column, Regexp#to_s (https://ruby-doc.org/core-2.5.1/Regexp.html#method-i-to_s) is an option:

    stored_expression = /.../.to_s

    To keep the pattern (source (https://ruby-doc.org/core-2.5.1/Regexp.html#method-i-source)) separate from its flags (options (https://ruby-doc.org/core-2.5.1/Regexp.html#method-i-options)) as strings:

    stored_pattern = /\A([^@\s]+)@((?:c-d+\.)+(in|com))\z/.source
    stored_flags   = /\A([^@\s]+)@((?:c-d+\.)+(in|com))\z/.options

    After you retreive those database values, reconstruct the regex object with the Regexp.new constructor (https://ruby-doc.org/core-2.5.1/Regexp.html#method-c-new):

    expression = Regexp.new(stored_expression)
    # alternatively
    expression = Regexp.new(stored_pattern, stored_flags)

    Consult the Ruby documentation for Regexp (https://ruby-doc.org/core-2.5.1/Regexp.html).

Other sides of the same question

  1. Escaping percent characters when calling String.Format?

    My strings.xml file holds a SQL query, and I'm trying to construct the finished string in code with String.Format. The SELECT…

    Replies 3Latest activity java
  2. Formatting strings with F#

    I'm building an F# library for the first time Using string.Format gives me a complaint saying that the function doesn't exist…

    Replies 2Latest activity fsharp
  3. Extracting one section of a tcl string

    With tcl I'm attempting to break apart Reports/Report#2.rpt and keep just Report#2.rpt ; I tried this approach, but it produc…

    Replies 2Latest activity tcl