We help IT Professionals succeed at work.

How do I capture my groups in my actionscript 3 regular expression?

396 Views
Last Modified: 2013-11-11
I have a string that has several occurrences of the expression i want to capture. my current expression seems to capture the entire string instead an array of matches.

i have arrays that are marked at the beginning with an x = and a y = . these arrays are the strings i am trying to capture from the "newString" string.

how do i correct this? attached is my code.
var newString = "fb 2 hole x = [0,7.95684,14.4188,19.9262,24.6842,28.8171,32.4129,35.5395,38.2391,40.557,42.5312,44.1871,45.5487,46.639,47.4761,48.0774,48.4561,48.627,48.6009,48.3891,48]; y = [0,-13.3809,-27.5554,-42.1324,-56.9716,-71.9945,-87.1577,-102.446,-117.79,-133.189,-148.656,-164.161,-179.682,-195.229,-210.788,-226.372,-241.934,-257.536,-273.113,-288.688,-304.3]; 1 hole x = [0,-6.36551,-11.5654,-16.0647,-20.0354,-23.5785,-26.7549,-29.6131,-32.1886,-34.512,-36.6049,-38.4899,-40.1776,-41.6884,-43.0312,-44.2169,-45.2553,-46.156,-46.9261,-47.5711,-48.1]; y = [0,-14.1012,-28.6959,-43.5136,-58.4794,-73.5648,-88.7259,-103.95,-119.218,-134.533,-149.875,-165.264,-180.644,-196.067,-211.501,-226.94,-242.384,-257.848,-273.326,-288.787,-304.3];";

var str:String = newString;
var resultArray:Array = [];

var matches:Array = str.match(/x = (\[.*\];)/); 
if (matches) 
{
trace("matches.length: " + matches.length); 
}

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
.* is greedy, so it will attempt to match as much as it can. Change it to be non-greedy by using

    .*?

This will tell the dot to only match as much as necessary to satisfy the pattern.  For more information, see (under the section "Watch out for Greediness"):  http://www.regular-expressions.info/repeat.html

A safer pattern would be:
var matches:Array = str.match(/x = (\[[^\]]*\];)/);

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
P.S.

    [^\]]*

means zero-or-more characters which are NOT the right square bracket.

Author

Commented:
it did capture the info correctly. right now it is capturing the "x =" portion as well as the expression in the parenthesis. i thought the parenthesis around the captured part is suppose to exclude the outer part of the expression. /x = (\[[^\]]*\];)
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
Parentheses do do that, but with respect to the CAPTURE GROUP. The overall match will still be what you specified by the pattern, but the capture group is like a sub-match. It seems as though match() will return the results of a match with capture groups as an array. Slot 0 (zero) of the array is the overall match (think entire pattern), slot 1 will be the first capture group, slot 2 the second, etc.

Author

Commented:
Ok I understand now. But I have 2 occurences of the x = .... pattern. I see that the 2nd occurence is being concatenated to the first occurence in the output....

matches[1]: [0,7.95684,14.4188,19.9262,24.6842,28.8171,32.4129,35.5395,38.2391,40.557,42.5312,44.1871,45.5487,46.639,47.4761,48.0774,48.4561,48.627,48.6009,48.3891,48];y = [0,-13.3809,-27.5554,-42.1324,-56.9716,-71.9945,-87.1577,-102.446,-117.79,-133.189,-148.656,-164.161,-179.682,-195.229,-210.788,-226.372,-241.934,-257.536,-273.113,-288.688,-304.3];1 hole x = [0,-6.36551,-11.5654,-16.0647,-20.0354,-23.5785,-26.7549,-29.6131,-32.1886,-34.512,-36.6049,-38.4899,-40.1776,-41.6884,-43.0312,-44.2169,-45.2553,-46.156,-46.9261,-47.5711,-48.1];y = [0,-14.1012,-28.6959,-43.5136,-58.4794,-73.5648,-88.7259,-103.95,-119.218,-134.533,-149.875,-165.264,-180.644,-196.067,-211.501,-226.94,-242.384,-257.848,-273.326,-288.787,-304.3];
I want the 2nd occurence to have its own position in the array. How would I alter the expression?
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
I'm not sure why you are seeing that behavior, but one thing you might try is adding the "global" (g) modifier to return more than one match per match() call:
var matches:Array = str.match(/x = (\[[^\]]*\];)/g);

Open in new window

Author

Commented:
maybe the expression should be limited or cut off at the "y =[....." after the first x = [...] array. it looks like the expression is still capturing too much until the end of the entire string.
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
Can you post what your code looks like and the exact result(s) you receive? I do not have Adobe development products installed, so I cannot test specifically, but testing in a PHP regex engine, the results I receive below.

The pattern I used was:  x = (\[[^\]]*\];)

The test text was:  fb 2 hole x = [0,7.95684,14.4188,19.9262,24.6842,28.8171,32.4129,35.5395,38.2391,40.557,42.5312,44.1871,45.5487,46.639,47.4761,48.0774,48.4561,48.627,48.6009,48.3891,48]; y = [0,-13.3809,-27.5554,-42.1324,-56.9716,-71.9945,-87.1577,-102.446,-117.79,-133.189,-148.656,-164.161,-179.682,-195.229,-210.788,-226.372,-241.934,-257.536,-273.113,-288.688,-304.3]; 1 hole x = [0,-6.36551,-11.5654,-16.0647,-20.0354,-23.5785,-26.7549,-29.6131,-32.1886,-34.512,-36.6049,-38.4899,-40.1776,-41.6884,-43.0312,-44.2169,-45.2553,-46.156,-46.9261,-47.5711,-48.1]; y = [0,-14.1012,-28.6959,-43.5136,-58.4794,-73.5648,-88.7259,-103.95,-119.218,-134.533,-149.875,-165.264,-180.644,-196.067,-211.501,-226.94,-242.384,-257.848,-273.326,-288.787,-304.3];
Array
(
    [0] => Array
        (
            [0] => x = [0,7.95684,14.4188,19.9262,24.6842,28.8171,32.4129,35.5395,38.2391,40.557,42.5312,44.1871,45.5487,46.639,47.4761,48.0774,48.4561,48.627,48.6009,48.3891,48];
            [1] => x = [0,-6.36551,-11.5654,-16.0647,-20.0354,-23.5785,-26.7549,-29.6131,-32.1886,-34.512,-36.6049,-38.4899,-40.1776,-41.6884,-43.0312,-44.2169,-45.2553,-46.156,-46.9261,-47.5711,-48.1];
        )

    [1] => Array
        (
            [0] => [0,7.95684,14.4188,19.9262,24.6842,28.8171,32.4129,35.5395,38.2391,40.557,42.5312,44.1871,45.5487,46.639,47.4761,48.0774,48.4561,48.627,48.6009,48.3891,48];
            [1] => [0,-6.36551,-11.5654,-16.0647,-20.0354,-23.5785,-26.7549,-29.6131,-32.1886,-34.512,-36.6049,-38.4899,-40.1776,-41.6884,-43.0312,-44.2169,-45.2553,-46.156,-46.9261,-47.5711,-48.1];
        )

)

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.