Regex 正则表达式和获取字符串的部分

Regex 正则表达式和获取字符串的部分,regex,Regex,我有很多类似的弦 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 theE

我有很多类似的弦

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
)


可以用正则表达式来实现这一点吗?如果是,怎么做?

我认为您可以使用正则表达式组和javascript String.match()方法,例如(第一个字符串):

结果将是一个数组:

["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()"]
现在,结果[0]包含完全匹配,但结果[1]…结果[n](n-捕获组数)包含参数在()和[]括号内的字符串。现在您可以拆分“float x,float y,float z”,“by”,“仅获取参数


您应该尝试泛化该模式,以便它将匹配第二个字符串和您拥有的其他字符串。这看起来有点疯狂,但这是我现在想到的唯一解决方案…

我想函数参数的数量是动态的?这是可能的。到目前为止你试过什么?>>我需要。。。请帮帮我。请指定输入格式。从您提供的两个示例中看不清楚。
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()"]