title: Ruby on Rails: Checking for valid regex does not work properly, high false rate
body: <p>In my application I've got a procedure which should check if an input is valid or not. You can set up a regex for this input.</p>
  
  <p>But in my case it returns false instead of true. And I can't find the problem.</p>
  
  <p>My code looks like this:</p>
  
  <pre><code>gaps.each_index do | i |
  if gaps[i].first.starts_with? "~"
    # regular expression      
      begin
       regex = gaps[i].first[1..-1]
       # a pipe is used to seperate the regex from the solution-string
       if regex.include? "|"
         puts "REGEX FOUND ------------------------------------------"
         regex = regex.split("|")[0..-2].join("|")
       end
       reg = Regexp.new(regex, true)
       unless reg.match(data[i])
       puts "REGEX WRONGGGG -------------------"
       @wrong_indexes &lt;&lt; i
       end
     rescue
    end
    else
     # normal string
     if data[i].nil? || data[i].strip != gaps[i].first.strip
       @wrong_indexes &lt;&lt; i
      end      
    end
  </code></pre>
  
  <p>An example would be:</p>
  
  <pre><code>[[~berlin|berlin]] 
  </code></pre>
  
  <p>The left one before the pipe is the regex and the right one next to the pipe is the correct solution. </p>
  
  <p>This easy input should return true, but it doesn't.</p>
  
  <p>Does anyone see the problem?</p>
  
  <p>Thank you all</p>
  
  <p>EDIT</p>
  
  <p>Somewhere in this lines must be the problem:</p>
  
  <pre><code>if regex.include? "|"
     puts "REGEX FOUND ------------------------------------------"
     regex = regex.split("|")[0..-2].join("|")
   end
   reg = Regexp.new(regex, true)
   unless reg.match(data[i])
  </code></pre>
  
  <p>Update: Result without ~ </p>
  
  <p><a href="https://i.stack.imgur.com/xhzOr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xhzOr.png" alt="enter image description here"></a></p>