1

I would like make a regex on controller (RoR). i have tried this code :

  @widget_check = ClanTemplatesWidget.find(params[:id])
  unless params[:config].blank?
    #@widget_check.config in database value = [{"input":"string","name":"code", "label":"Code clan","validation":"^([#][A-Z0-9]{7})$"}]
    JSON.parse(@widget_check.config).each do |config,i|
     regexp = Regexp.new(config["validation"])
     if regexp.match(params[:config][config["name"]])
       @error = 0
     else
       @error = 1
     end
    end
    @widget.config = params[:config].to_json
  end

For information my data is on the database. If i inspect the two params i have that :

config["validation"] = /^([#][A-Z0-9]{8})$/

params[:config][config["name"]] = #2YL9GR9R

If i rewrite my code with the data it's work fine (like that) :

if /^([#][A-Z0-9]{8})$/.match("#2YL9GR9R")
  @error = 0
else
  @error = 1
end
5
  • Since #2YL9GR9R doesn't have quotes, I would guess that both values (params[:config][config["name"]] and config["validation"]) are in fact strings. Commented Oct 12, 2016 at 6:58
  • What exactly is the problem that you have? Commented Oct 12, 2016 at 7:00
  • How do you set config["validation"]? Commented Oct 12, 2016 at 7:22
  • In database it's a json form input. Commented Oct 12, 2016 at 7:48
  • You are saving your config values in a database, right? But how / where do you set the config variable / hash? You have to somehow retrieve the values from the database and assign them to config. Commented Oct 12, 2016 at 8:27

1 Answer 1

3

Your config["validation"] contains a string that looks like a regexp, but it is not. But you can build a regexp out of a string:

regexp = Regexp.new(config["validation"])
regexp.match(params[:config][config["name"]])

Since Regexp.new expects a string without the surrounding /.../, you just might want to omit them.

Sign up to request clarification or add additional context in comments.

12 Comments

Or you could use params[:config][config["name"]].match(config["validation"]) – String#match implicitly converts the argument to a Regexp. (the surrounding /.../ must be removed in either case)
Thank you for your answer, i've tried your code but still not working.
@Shinix did you remove the leading and trailing /? You have to store "^([#][A-Z0-9]{7})$" instead of "/^([#][A-Z0-9]{7})$/"
Yes, i've tried without / (i've update the code on my first post). If i use config data it's not working but if i use text like that it's work :/ regexp = Regexp.new("/^([#][A-Z0-9]{8})$/") regexp.match("#2YL9GR9R")
@Shinix double check (within your code) if config["validation"] == "^([#][A-Z0-9]{8})$" and if params[:config][config["name"]] == "#2YL9GR9R". If they don't match, print both values via p value or puts value.inspect. Maybe there's a trailing newline or another minor difference.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.