简体   繁体   English

结合Regexp和值集(散列/数组)来比较字符串是否与ruby匹配

[英]Combine Regexp and set of values (hash/array) to compare if a string matches in ruby

I have the following pattern to check: 我有以下模式来检查:

"MODEL_NAME"-"ID"."FORMAT_TYPE"

where, for example: 在哪里,例如:

MODEL_NAME = [:product, :brand]
FORMAT_TYPE = [:jpg, :png]

First I wanted to check if the regexp is something like: 首先,我想检查正则表达式是否类似于:

/^\w+-\d+.\w+$/

and I have also to check if the part of my string is part of my arrays. 我还要检查我的字符串的一部分是否是我的数组的一部分。 I want something more flexible than: 我想要比以下更灵活的东西:

/^(product|brand)-\d+.(jpg|png)$/

which I could manage through my arrays. 我可以通过我的阵列管理。 What is a good solution to do it? 这样做的好方法是什么?

/^(#{MODEL_NAME.join '|'})-\d+\.(#{FORMAT_TYPE.join '|'})$/
# => /^(product|brand)-\d+\.(jpg|png)$/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM