2016-12-01 33 views
1

我試圖用圖標替換數據中的某些符號。正則表達式問題 - 雙匹配號碼

我目前的RegEx是/\{.+?\}|\+\d+|\−\d+|\{[T]}|X|(0:)*/ 這實際上擊中了我擁有的每一個樣品。

一些樣品

+2 −2 {2} 

等等等等

然而,它還可選配+2/+2並用符號代替每個+2+2

現在,在我的每個示例中,我使用+2,但它可以是1-100(大多數爲1-10)中的任何數字。 我會發布Rails Helper我用它來交換。我嘗試了約20個不同版本的[\+(0-9\/\+0-9)],其中包括一個txt2re建議 - ([-+]\\d+\/[-+]\\d+)但我或者A)失去了工作效果B)沒有做任何事情。

我歡迎任何建議。將發佈3個確切的文本樣本,爲我的一些問題有看法。

例子

`+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."` 

+1,-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,很難理解你需要什麼。試試'/[-+]\d+\/[-+]\d+|\{.+?\}|\+\d+|\-\d+|\{[T]}|X|(0:)* /'。然而,我不明白爲什麼你有'(0:)*'匹配任何空的位置之前不匹配的符號。 –

+0

@WiktorStribiżew你是什麼意思?我提供了3個示例 - 其中一個失敗,每次我嘗試不同的變體。 您的解決方案實際上似乎工作。你會把它作爲答案與解釋,我會檢查一些其他卡片,並將其標記爲答案?謝謝。 – 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),*請記住,正則表達式引擎是渴望*部分。 –