2016-03-19 10 views
-2

私はcarrierwaveをデバッグしています。それは楽しいです。私は、問題はこれに尽きると思う:Regex fun。なぜこの正規表現は私の文字列にマッチしないのですか?

if record.new_record? && record.send("has_#{attribute}_upload?") && record.key !~ record.send(attribute).key_regexp 

#key_regexpコードはこれです:carrierwave_directインサイド

、このコードがあります

私は、デバッグしていたときに、このと評価さ
/\A#{store_dir}\/[a-f\d\-]+\/.+\.(?i)#{extension_regexp}(?-i)\z/ 

record.send(attribute).key_regexp 
/\Astories\/\/main_images\/[a-f\d\-]+\/.+\.(?i)(jpg|jpeg|gif|png)(?-i)\z/ 

私store_dirはこれです:

"の物語/#{} model.id/main_images"

と私はmodel.idがnilであるため、空白 "/" セクションがあると思います。

"/stories/main_imagescache/20160319-1549-2202-5826/blueapronimage.jpg"

なぜそれが現在一致していません:

record.keyはこれですか?そして、 " - 「」

[a-f\d\-]+\/.+\.(?i)(jpg|jpeg|gif|png)(?-i) 

それはAF、数字、または間の任意の数の文字です:正規表現は、余分なスラッシュを持っていると私は何をこのセクションがあることはよく分からないように見えます

/"、任意の数の文字または数字、"。 "....

(?i)とは何ですか?

キャプチャグループとは何ですか?

(?-i)とは何ですか?

私は、これが問題だと思う:

record.key !~ /\Astories/ 
true 

それはfalseにする必要があります。この時

+2

[RegExr](http://regexr.com/)または[Regex101](https://regex101.com) /)。部品の上にマウスを置くと、それが何をするかが正確に伝えられます。彼らはどちらも*あなたのためのRegExを説明する* Explain *セクションを持っています。 – Druzion

答えて

0

見て、それを説明する:それぞれが何をするかを説明しなければならない

[a-f\d\-]+   # Matches a-f, any digit, - (hyphen) 
         #^One or more times (+) 
\/     # Matches a/
.+     # Matches any character one or more times 
\.     # Matches a . (dot) 
(?i)     # States that anything after this is case-insesitive 
         #^Inline Flag 
(jpg|jpeg|gif|png) # Selects file extension 
         #^(either jpg, jpeg, gif, or png) 
(?-i)     # Removes Inline Flag 

また、Live Demoもチェックしてください。あなたの例は一致しているようですが、コードの別の部分が間違っているかもしれません...

+0

明らかに、これは間違った振る舞いです:record.key!〜/ \ Astories /それはtrueを返します – Jwan622

+0

私はライブデモでそれを入れました、うまくいくように見えます[**参照?**](https:// regex101.com/r/jD4pK4/2) – Druzion

関連する問題