2012-07-20 88 views
-3

的越來越部分我有很多像琴絃正則表達式和字符串

blip createBlip (float x, float y, float z, [int icon=0, int size=2, int r=255, int g=0, int b=0, int a=255, int ordering=0, float visibleDistance=99999.0, visibleTo = getRootElement()]) 

float float float getElementRotation (element theElement [, string rotOrder = "default" ]) 

我需要得到份該字符串的,像

array(
[1] = blip 
[2] = createBlip 
[3] = x 
[4] = y 
[5] = z 
) 

array(
[1] = blip createBlip 
[2] = float x 
[3] = float y 
[4] = float z 
[5] = int icon=0 
[6] = int size=2 
[7] = int r=255 

.. 。

用正則表達式可以做到這一點嗎?如果是,如何?

+0

我想函數參數的數量是動態的嗎?有可能的。你試過什麼了? – 2012-07-20 17:34:42

+0

>> ***我需要......請幫助我。*** <<向我們展示您的代碼(到目前爲止)。 – 2012-07-20 17:39:43

+0

請指定輸入格式。從你提供的兩個例子中可以看不出來。 – oberlies 2014-05-08 15:00:09

回答

1

我認爲你可以用正則表達式組和javascript String.match()方法,例如(1弦)做到這一點:

var string = "blip createBlip (float x, float y, float z, [int icon=0, int size=2, int r=255, int g=0, int b=0, int a=255, int ordering=0, float visibleDistance=99999.0, visibleTo = getRootElement()])"; 

var result = string.match(/((?:(?:\w+)\s?)+?)\(?\[?((?:(?:(?:(?:\s?\w+)+))\,)+)\s?\[((?:(?:(?:\s?=?\.?\(?\)?\w?)+)\,?)+)\]\s?\)/); 

結果將是一個數組:

["blip createBlip (float x, float y, float z, [int icon=0, int size=2, int r=255, int g=0, int b=0, int a=255, int ordering=0, float visibleDistance=99999.0, visibleTo = getRootElement()])", "blip", "createBlip", " float x, float y, float z,", "int icon=0, int size=2, int r=255, int g=0, int b=0, int a=255, int ordering=0, float visibleDistance=99999.0, visibleTo = getRootElement()"] 

現在,result [0]包含完全匹配,但結果[1] .. result [n](n - 捕獲組的數量)包含帶有()和[]括號內參數的字符串。現在你可以用「,」分隔「float x,float y,float z」,只獲取參數。

您應該嘗試概括該模式,以便它匹配第二個字符串和其他字符串。它看起來有點瘋狂,但它現在是我唯一想到的解決方案...