我正在尝试用图标替换数据中的某些符号。
我目前的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是特定的,但它只是为我生成哈希,然后我再说出来。
答案 0 :(得分:1)
您似乎尝试添加模式以匹配正则表达式末尾的2个斜杠分隔数字,而您需要在其开始处执行此操作,因为非锚定中的替代顺序模式很重要:
-
请参阅Rubular demo
另请注意,角色类外的\{.+?\}
不必转义。
和FYI:{zzz{yyy{xxx}
匹配字符串,就像\{[^{}]+\}
一样。如果您需要避免这种情况,请使用import json
with open(path, 'r') as infile:
data = json.load(infile)
。