Search code examples
phparraysregexlinden-scripting-language

regex array multiple parentheses


Hello I have api function which is basically text in my database

here are the functions I use

alpha(integer link, integer face, float alpha);
color(integer link, integer face, vector color, float alpha);
description(integer link, string description);
glow(integer link, integer face, glow);
name(integer link, string name)
position(integer link, vector position)
rotation(integer link, vector rotation)
texture(integer link, integer face, string texture_uuid, vector repeats, vector offsets, float rotation_in_radians)

basically what I looking to do is to run a regex to return each of these as array as I tried many times and not having much luck for example in textures I need pick the uuid key which I was using

([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12})

which works fine for that one field but since I have other fields it does not seem to get into array as I not sure how to run multipliable regex

also the link and face can also be negative values as I was using (<[^>]*>|\w+) which works to a degree but not all the api functions

any idea how I can build regex to work on all api function types as I need them in array like this

array(
[0] = function,
[1] = link
[2] = face
[3] = texture uuid
[4] = texture repeats
[5] = offset
[6] = rotation
)

this is example using the texture function I hope I explained this enough as I having hard time here and looking for help

here example I was using

https://regex101.com/r/vO2gW1/3 as you see its broken as don't pick up uuid

or is there a way with regex or another method to execute these as functions even and keeping there structure as functions to return the data in format I need it for using PHP


Solution

  • You can try

    ^[^(]+(?=\()|(?<=[(,])\s*\K(?:<.*?>|.*?)(?=[,)])
    

    It should pick up everything you need. Demo.

    What it does is to match anything between the start of the string, commas and parentheses. So it won't work with stuff like anim(foo()).