Question

last night I decided to learn regex in order to create a "test" script parser and I've successfully done so in terms of variables :)

This is the AS3 regexp that I use to parse variables (not sure if this can be simplified but this is what came into my mind):

/\s*>(\w+)\s*=\s*(?:(?:"(.*?)")|>(\w+))\s*/gsm

The parsed variables take this format (example 1):

>ReferenceTestVariable = "Hello World"

or.. (example 2):

>TestVariable2 = >ReferenceTestVariable

The problem is with the groupings and I don't know how to word this properly but this is my regexp output for example 1 as an array

 [1]=ReferenceTestVariable, [2]=Hello World, [3]=

And this is the output for example 2:

 [1]=TestVariable2, [2]=, [3]=ReferenceTestVariable

How would I modify the group operator after the = to make it so index [2] and [3] are joined seeing as both index 2 and 3 are the values the variable stores? Also how would I modify the regexp to allow something like this:

>Test = "String", >Referencevariable, "Another String", "More strings", >Variable2
Was it helpful?

Solution

I think this should do it:

/\s*>(\w+)\s*=\s*(".*?"|>\w+)/gsm

index[2] will have quotes around it in case 1, or begin with > in case 2.

To do the second, it would be:

/\s*>(\w+)\s*=\s*((?:(?:".*?"|>\w+)\s*,\s*)*(?:".*?"|>\w+))/gsm

However, when you do this, all the comma-separated values will be together in index[1]. Regular expression engines don't generally provide a way to get each of the values matched by a * or + separately.

DEMO

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top