所以我一直在做一个小项目,为游戏的脚本语言编写语法突出显示器。一切都顺利进行,除了一部分:数字。
以这些线条为例
(5:42) Set database entry {healthpoints2} to the value 100.
(5:140) Move the user to position (29,40) on the map.
我想在最后突出显示 100,而不突出显示大括号中的 (5:42) 或 2。数字不会总是在同一个地方,也不会总是只有一个数字。
我基本上需要一个正则表达式来表示:
"匹配不在 {} 之间的任何数字,并且不匹配 (#:#) 模式。"
我已经在这里呆了一天半了,我正在拔头发试图弄清楚。对此的帮助将不胜感激!我已经看过 regular-expressions.info,并尝试使用RegexBuddy,但我只是不明白:c
编辑:根据要求,这是直接从脚本编辑器复制的更多行。
(0:7) When somebody moves into position (**10** fhejwkfhwjekf **20**,
(0:20) When somebody rolls exactly **10** on **2** dice of **6** sides,
(0:31) When somebody says {...},
(3:3) within the diamond (**5**,**10**) - **20** //// **25**,
(3:14) in a line starting at (#, #) and going # more spaces northeast.
(5:10) play sound # to everyone who can see (#,#).
(5:14) move the user to (#,#) if there's nobody already there.
(5:272) set message ~msg to be the portion of message ~msg from position # to position #.
(5:302) take variable %var and add # to it.
(5:600) set database entry {...} about the user to #.
(5:601) set database entry {...} about the user named {...} to #.
当你看到这个解决方案时,你可能会踢自己......
假设这个所需的数字将始终在句子中使用,它应该始终在它前面有一个空格。
$pattern = '/ [0-9]+/s';
如果前面的空间并不总是存在,请告诉我,我会更新答案。
这是更新的正则表达式,以匹配您问题中的 2 个示例:
$pattern = '/[^:{}0-9]([0-9,]+)[^:{}0-9]/s';
第三次更新以考虑您的问题修订:
$pattern = '/[^:{}0-9a-z#]([0-9]+[, ]?[0-9]*)[^:{}0-9a-z#]/s';
所以你不会在诸如
{更新 29 测试}
您可能需要预先去除牙套,如下所示:
$pattern = '/[^:{}0-9a-z#]([0-9]+[, ]?[0-9]*)[^:{}0-9a-z#]/s';
$str = '(0:7) Hello {update 29 testing} 123 Rodger alpha charlie 99';
$tmp_str = preg_replace('/{[^}]+}/s', '', $str);
preg_match($pattern, $tmp_str, $matches);
('d+,'s?'d+)|(?<!['('{:]|:'d)'d+(?![')'}])
http://regexr.com?30omd
这行得通吗?