preg_replace 1 行上的多个实例?
我从扑克软件中得到了这样的文本(我已经突出显示了需要更换的部分)。
--- 失败 --- [Jh 9h Ah]
驾驶员座椅检查
达林顿还剩 15 秒采取行动
达灵顿投注 100
InvisibleEnigma 拨打 100
驾驶座折叠
--- 转动 --- [Jh 9h Ah] [3c]
达灵顿投注 200
InvisibleEnigma 拨打 200
--- 河 --- [Jh 9h Ah 3c] [Td]Jh = 红心杰克
9h = 红心 9
啊=红桃王牌
3c = 3 梅花
我想用 IMAGES 替换方括号内的牌。
所以这一行: --- TURN --- [Jh 9h Ah] [3c]
需要变成: --- TURN --- jh.gif 9h.gif ah.gif 3c.gif
我无法弄清楚 preg_replace :( 我可以通过括号内的一张卡来弄清楚(就像[3c]),但我坚持替换 1 行上的多个实例,一些括号有 3 张卡,一些括号有 2 或 1 张卡,
这就是我对单张卡的情况:
\[([AKQJakqj1-9]0?[DHSCdhsc])\]
任何帮助将不胜感激。
I have text like this from poker software (I have highlighted the parts I need to replace).
--- FLOP --- [Jh 9h Ah]
driverseati checks
darrington has 15 seconds left to act
darrington bets 100
InvisibleEnigma calls 100
driverseati folds
--- TURN --- [Jh 9h Ah] [3c]
darrington bets 200
InvisibleEnigma calls 200
--- RIVER --- [Jh 9h Ah 3c] [Td]Jh = Jack of hearts
9h = 9 of hearts
Ah = ace of hearts
3c = 3 of clubs
I want to replace the cards inside the square brackets with IMAGES.
So this line: --- TURN --- [Jh 9h Ah] [3c]
Needs to become: --- TURN --- jh.gif 9h.gif ah.gif 3c.gif
I can't figure out the preg_replace :( I can kind of figure it out for just a single card inside brackets (like the [3c]), but I'm stuck at replacing multiple instances on 1 line and some brackets having 3 cards and some brackets having 2, or 1 card.
This is what I have for a single card:
\[([AKQJakqj1-9]0?[DHSCdhsc])\]
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该代码输出:
Looks like John (narrowly!) beat me to it while I hacked away, although my code here is a bit more specific - it won't run the replacement on any string inside brackets, like
[as the world turns]
, it has to be a set of cards. I also limited it to 5 cards in brackets, although you could change this by simply adjusting the{0,4}
in the match.This code outputs:
Looks like John (narrowly!) beat me to it while I hacked away, although my code here is a bit more specific - it won't run the replacement on any string inside brackets, like
[as the world turns]
, it has to be a set of cards. I also limited it to 5 cards in brackets, although you could change this by simply adjusting the{0,4}
in the match.如果您正在寻找方括号(正如您所发现的那样),则很难做到。如果您不尝试匹配它们而只是查找卡片名称(无论它们出现在何处),那么就很简单:
\b
标记检查单词边界。它们确保卡片名称必须是不同的单词,因此例如Ahead
中的Ah
不会匹配。处理方括号相当复杂。理想情况下,您可以使用 lookahead 和lookbehind 断言 来检查它们,但是不幸的是,向后断言有一个限制,这使得它们不适合这项工作:
因此,我想出了一种双重替换解决方案,使用
/e
标志将一个preg_replace
调用嵌套在另一个调用中。外部正则表达式查找方括号并捕获内部。内部正则表达式在
$1
内查找卡片名称并将其转换为图像。顺便说一句,我将
T
添加到您的模式中,因为 10♦ 通常在手牌历史记录中写为Td
,并且我将i
标志添加到使搜索不区分大小写。It's difficult to do if you're looking for the square brackets (as you're finding out). If you don't try to match them and simply look for the card names, wherever they occur, then it's a cinch:
The
\b
markers check for word boundaries. They ensure that the card name must be a distinct word, soAh
inAhead
won't match, for example.Handling the square brackets quite complicated. Ideally you could use lookahead and lookbehind assertions to check for them, but there's an unfortunate restriction on lookbehind assertions that makes them unsuitable for the job:
So instead I came up with a double-replace solution using the
/e
flag to nest onepreg_replace
call inside of another.The outer regex looks for square brackets and captures the insides. The inner regex looks for card names inside of that
$1
and turns them into images.By the way, I added
T
to your pattern since 10♦ is typically written asTd
in hand histories, and I added thei
flag to make the searches case-insensitive.