Reputation: 137
I'm not really a stranger to RegEx, I get the general idea behind it and have used it before with good results. But this one has my blood boiling, simply due to the amount of square brackets and single quotation marks.
I get one part working, another part breaks. I can't seem to come up with a way for it to replace it all in one go, so I thought I'd see if there are any kind souls out there that can help a desperate man.
Basically what I have are hundreds of entries that look like this;
_newObject = createVehicle ['Land_CncWall4_F', [11459.2,11330.3,-0.000549316], [], 0, 'CAN_COLLIDE'];
_newObject setPosASL [11459.4,11330.2,317.238];
_newObject setVectorDirAndUp [[0.790714,-0.612186,0], [0,0,1]];
What I want to do is, I need to get the name of the object, in this case Land_CncWall4_F. As well as the setPosASL and VectorDirAndUp co-ordinates and then format this into a new array.
The new array would look like this
["Land_CncWall4_F", [11459.4, 11330.2, 317.238], [[0.790714, -0.612186, 0], [0, 0, 1]]];
I managed to extract the object's name into a new line using backreference, which worked nicely, but if I try and get the co-ordinates as well I just get lost in my own abomination of regex.
Edit:
I did manage to make a little more progress this time, and as requested this is what I've got so far;
// Find
_newObject = createVehicle \['(.*)', \[.*], 0, 'CAN_COLLIDE'];
// Replace
\n["\1"]
// Outcome
["Land_CncWall4_F"]
Now to try and do the rest as well. Oh RegEx how I both love and utterly despise you :P
Edit2:
Thanks to all the helpful people this has now been solved! I ended up with this;
// Find
_newObject = createVehicle\s*\['(\w+)'.+\R_newObject setPosASL\s*(\[.+?\]);\R_newObject setVectorDirAndUp\s*(\[\[.+?\]\]);
// Replace
["$1", $2, $3, false],
Final output looks like this;
["Land_CncWall4_F", [11449.9,11318.2,317.056], [[0.780067,-0.625696,0], [0,0,1]], false],
Which is exactly what I wanted. Thanks everyone for your help, saved me lots of painful hair loss ;)
Upvotes: 1
Views: 102
Reputation: 14228
This regex _newObject setPosASL\s+(\[.*\]);$
will give the group in parentheses the required [11459.4, 11330.2, 317.238]
This regex _newObject setVectorDirAndUp\s+(\[\[.*\]\]);$
will give the group in parantheses the required [[0.790714,-0.612186,0], [0,0,1]]
Because I assume you got the Land_CncWall4_F, lets say it is group1, you can concatenate the groups like ["group1", group2, group3]
example for first one in notepad++ looks like:
Upvotes: 1
Reputation: 43169
This might help you out:
(?s)\['(?<object>[^']+)'.*?setPosASL\s*(?<setPosASL>[^;]+).*? setVectorDirAndUp\s*(?<VectorDirAndUp>[^;]+)(?s-)
The trick is to use the single-line mode and look for the commands literally. See a demo on regex101.com. You can obviously ommit the naming groups and have them sorted as usual (1-x
).
Upvotes: 1
Reputation: 91385
How about:
Find what: createVehicle\s*\['(\w+)'.+\R_newObject setPosASL\s*(\[.+?\]);\R_newObject setVectorDirAndUp\s*(\[\[.+?\]\]);
Replace with: ["$1", $2, $3]
Your input string:
_newObject = createVehicle ['Land_CncWall4_F', [11459.2,11330.3,-0.000549316], [], 0, 'CAN_COLLIDE'];
_newObject setPosASL [11459.4,11330.2,317.238];
_newObject setVectorDirAndUp [[0.790714,-0.612186,0], [0,0,1]];
becomes:
_newObject = ["Land_CncWall4_F", [11459.4,11330.2,317.238], [[0.790714,-0.612186,0], [0,0,1]]]
Upvotes: 1