2012-04-24 53 views
1

所以我一直在爲一個小項目編寫一個遊戲腳本語言的語法高亮器。這一切都沒有阻礙,除了一部分:數字。PHP:preg_replace匹配一些數字,但不匹配

把這些線例如

(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 #.

+0

你是什麼意思'突出'?請至少提供一個輸入示例和預期輸出。 – Madbreaks 2012-04-24 22:26:09

回答

1

當你看到這個解決方案你可能會踢自己...

假設這所需數量將永遠是用過的 在一個句子中,它應該總是在它之前有一個空格。

$pattern = '/ [0-9]+/s'; 

如果前面的空間並不總是存在,讓我知道,我會更新的答案。


下面是更新正則表達式來匹配你的問題的2個例子:

$pattern = '/[^:{}0-9]([0-9,]+)[^:{}0-9]/s'; 

3ND更新以解決你的問題的修訂:

$pattern = '/[^:{}0-9a-z#]([0-9]+[, ]?[0-9]*)[^:{}0-9a-z#]/s'; 

所以你不」 t突出顯示的數字,如

{更新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); 
+0

不幸的是,就像我剛剛在帖子中編輯過的那樣,有時候這些數字會出現在(#,#)設置或週期中,或者與周圍的任何其他數量的奇怪事物有關。 - 這個腳本的目的是爲了讓用戶提交的代碼更加美觀,所以我試圖用它輸入的任何奇怪的東西來工作。 – 2012-04-24 22:18:02

+0

好的,請你發佈一個更廣泛的例子嗎?我需要看到什麼應該匹配的全部範圍... – HappyTimeGopher 2012-04-24 22:19:32

+0

剛剛看到新的例子... 1秒... – HappyTimeGopher 2012-04-24 22:24:35

0
(\d+,\s?\d+)|(?<![\(\{:]|:\d)\d+(?![\)\}]) 

http://regexr.com?30omd

將這項工作?

+0

不完全,它仍然使得{}內的數字突出顯示。 – 2012-04-24 22:41:07