2016-12-01 26 views
1

私はアイコンでデータ内の特定のシンボルを交換しようとしています。RegExの問題 - 二重一致番号

現在の正規表現は/\{.+?\}|\+\d+|\−\d+|\{[T]}|X|(0:)*/ これは実際に私が持っているすべてのサンプルに当てはまります。

いくつかのサンプル

+2 −2 {2} 

などなど

しかし、それはまた+2/+2に一致し、 +2のシンボルと各+2を交換です。

私の各例では+2を使用していますが、1-100(ほとんど1-10)の任意の数字を使用できます。 Rails Helper私はそれを交換するために使用します。私は約012種類のバージョンの[\+(0-9\/\+0-9)]を試しましたが、それはtxt2reが提案したものを含めて - ([-+]\\d+\/[-+]\\d+)ですが、私はどちらもA)働いていた効果を失いました。B)何もしなかった。

私は何か提案を歓迎します。 3つの正確なテキストサンプルを投稿して、私の問題のいくつかを見通しにします。

例1

`+1: Tap target permanent. It doesn't untap during its controller's next untap step. −2: Draw a card for each tapped creature target player controls. −8: You get an emblem with "You have no maximum hand size" and "Whenever a card is put into your graveyard from anywhere, you may return it to your hand."` 

、-8また、それが正常でない(アイコンになるはず - 。。。記号はなく、より広いわからない、技術的な名前だに見て何か

Equipped creature gets +1/+1 for each color among permanents you control. As long as Conqueror's Flail is attached to a creature, your opponents can't cast spells during your turn. Equip {2} 

/+ 1 + 1は、変異体番号であるが、ほとんどの場合、この形式になりますしかし、時にはそれが*/+1又は+1/*であってもよい - 。わからないどのようにこれを解くために

+2: Look at the top card of target player's library. You may put that card on the bottom of that player's library. 
0: Draw three cards, then put two cards from your hand on top of your library in any order. −1: Return target creature to its owner's hand. −12: Exile all cards from target player's library, then that player shuffles his or her hand into his or her library. 

これは問題のサンプルの大部分です。

マイヘルパー

def card_text_swap card 
    if card.nil? 
     return 
    else 
    pos_entries = (1..20).map do |i| 
     ["+#{i}", "<br /><i class=\"ms ms-loyalty-up ms-loyalty-#{i}\"></i>"] 
    end.to_h 
    neg_entries = (1..20).map do |i| 
     ["−#{i}", "<br /><i class=\"ms ms-loyalty-down ms-loyalty-#{i}\"></i>"] 
    end.to_h 
    power_tough = (1..20).map do |i| 
     ["+#{i}/", "<span></span>"] 
    end.to_h 
    cost_entries = (1..20).map do |i| 
     ["{#{i}}", "<i class=\"ms ms-#{i} ms-cost ms-shadow\"></i>"] 
    end.to_h 
    hash = { '{hw}' => '<span class="ms-half"><i class="ms ms-w ms-cost"></i></span>', 
       '{W}' => '<i class="ms ms-w ms-cost ms-shadow"></i>', 
       '{R}' => '<i class="ms ms-r ms-cost ms-shadow"></i>', 
       '{U}' => '<i class="ms ms-u ms-cost ms-shadow"></i>', 
       '{G}' => '<i class="ms ms-g ms-cost ms-shadow"></i>', 
       '{B}' => '<i class="ms ms-b ms-cost ms-shadow"></i>', 
       '{S}' => '<i class="ms ms-s ms-cost ms-shadow"></i>', 
       '{X}' => '<i class="ms ms-x ms-cost ms-shadow"></i>', 
       'X' => '<i class="ms ms-x ms-cost ms-shadow"></i>', 
       '{W/U}' => '<i class="ms ms-wu ms-split ms-cost"></i>', 
       '{W/B}' => '<i class="ms ms-wb ms-split ms-cost"></i>', 
       '{W/P}' => '<i class="ms ms-wp ms-cost ms-cost"></i>', 
       '{2/W}' => '<i class="ms ms-2w ms-split ms-cost"></i>', 
       '{U/B}' => '<i class="ms ms-ub ms-split ms-cost"></i>', 
       '{U/R}' => '<i class="ms ms-ur ms-split ms-cost"></i>', 
       '{U/P}' => '<i class="ms ms-up ms-cost ms-cost"></i>', 
       '{2/U}' => '<i class="ms ms-2u ms-split ms-cost"></i>', 
       '{B/R}' => '<i class="ms ms-br ms-split ms-cost"></i>', 
       '{B/G}' => '<i class="ms ms-bg ms-split ms-cost"></i>', 
       '{B/P}' => '<i class="ms ms-bp ms-cost ms-cost"></i>', 
       '{2/B}' => '<i class="ms ms-2b ms-split ms-cost"></i>', 
       '{R/G}' => '<i class="ms ms-rg ms-split ms-cost"></i>', 
       '{R/P}' => '<i class="ms ms-rp ms-cost ms-cost"></i>', 
       '{R/W}' => '<i class="ms ms-rw ms-split ms-cost"></i>', 
       '{2/R}' => '<i class="ms ms-2r ms-split ms-cost"></i>', 
       '{G/W}' => '<i class="ms ms-gw ms-split ms-cost"></i>', 
       '{G/B}' => '<i class="ms ms-gb ms-split ms-cost"></i>', 
       '{G/P}' => '<i class="ms ms-gp ms-cost ms-cost"></i>', 
       '{2/G}' => '<i class="ms ms-2g ms-split ms-cost"></i>', 
       '0:' => '<br /><i class="ms ms-loyalty-zero ms-loyalty-0"></i>', 
       '{T}' => '<i class="ms ms-tap"></i>', 
       '{P}' => '<i class="ms ms-tap"></i>', 
       '{C}' => '<i class="ms ms-c"></i>', 
       '\n' => '<br>' 
       } 
    cost_entries = cost_entries.merge(neg_entries) 
    cost_entries = cost_entries.merge(pos_entries) 
    cost_entries = cost_entries.merge(power_tough) 
    hash = hash.merge(cost_entries) 
    card.gsub(/\{.+?\}|\+\d+|\−\d+|\{[T]}|X|(0:)*|(\+[0-9]\/)/) { | k | hash[k] || k }.html_safe 
    end 
    end 

Railsが特定されますが、ちょうど私がそれから出GSUBことを、私のためのハッシュを生成します。

+0

MCVEがなければ、必要なものを理解することは難しいです。試してみてください//'しかし、私は '(0:)*'が一致しないシンボルの前に空の場所にマッチする理由は分かりません。 –

+0

@WiktorStribiżewどういう意味ですか?私は3つの例を提供しました - 私は別の変種を試すたびにそれらの1つが失敗します。 あなたのソリューションは実際には動作しているようです。あなたは解説付きの解答としてそれを入れて、他のカードをチェックしてそれを答えにしますか?ありがとう。 – DNorthrup

+0

さて、私は問題を参照して、私は説明と答えを投稿した。 –

答えて

1

非アンカーパターンにおける選択肢の順序が重要ので、あなたがその開始でそれを行う必要がある一方で、正規表現の末尾に2スラッシュで区切られた数字と一致するパターンを追加しようとしたようだ:

/[-+]\d+\/[-+]\d+|\{.+?\}|\+\d+|-\d+|\{T\}|X|(0:)*/ 
^^^^^^^^^^^^^^^^^ 

文字クラス外-をエスケープする必要はありませんのでご注意、またRubular demo

を参照してください。

FYI:\{.+?\}は、{zzz{yyy{xxx}のような文字列にも一致します。これを避ける必要がある場合は、\{[^{}]+\}を使用してください。

+0

私は、RegExの注文がどのように重要であるかについてはあまりよく分かりませんでした。私が苦労していたもう一つの要素でした。私はカードを点検して、 '\ {。+?\}'に問題があるかどうかを確認します。 – DNorthrup

+1

[regular-expressions.info(http://www.regular-expressions.info/alternation.html)にある[alternations]の詳細については、* Regex Engine Is Eager *セクションを参照してください。 –

関連する問題