10 Replies Latest reply on Mar 10, 2012 12:35 PM by sinious

    Bit of RegEx help

    TOverturf Level 1

      Hey,

       

      I have a string variable that looks something like this:

       

      2012-03-07 135.06 135.91 134.93 135.69 143616800 135.69

      2012-03-06 135.35 135.43 134.36 134.75 202129900 134.75

       

      It continues like this, a list of reoccurring variables seperated by spaces, for a few more lines.

       

      So here my question: How can I separate the string and push the different variables into their appropriate arrays. The end result should look like this:

       

      dates = [2012-03-07, 2012-03-06]

      variable1 = [135.06, 135.35]

      variable2 = [135.91, 135.43]

       

      and so on.

       

      Thanks in advance.

        • 1. Re: Bit of RegEx help
          Ned Murphy Adobe Community Professional & MVP

          What of what you show is a string variable, is the whole thing one variable, or is each line a separate variable?

           

          I think using String.split() operations provide a more appropriate approach than trying to use regular expressions

          • 2. Re: Bit of RegEx help
            TOverturf Level 1

            Whole thing is one variable. I'm not very familiar with the split operation, could you elaborate?

            • 3. Re: Bit of RegEx help
              Ned Murphy Adobe Community Professional & MVP

              If you want to become familiar with any method or property or other aspect of any Flash class the Flash help documentation is one of the best tools available to you.  If you type "String" into the actions panel, then select it, then right click it and choose "View Help", the String section of the documentation will open and you can look thru the methods section and find the split() method explained in detail.

               

              In short, the split() method can be used to divide a string based on a delimiter (character, string, or regex), the result of which is an array cpontaining the split pieces.  In your string you have new line characters that separate each set of records, and then you have spaces that separate the detail data of each record.  So you could use the split method to first split the whole string into the records, and then you can perform another operation to split each record into its details and store them into their own arrays as you say you want.  The processing could be something like the following, where your original string is captured in the variable named dataString...

               

              var dataString:String = .... your complete string

               

              var dataSets:Array = dataString.split("\r\n"); // split the string by its new line characaters

               

              // now break down the dataSets records

               

              for(var i:uint=0; i<dataSets.length; i++){

                var tempArray:Array = dataSets[i].split(" "); // split the record by its spaces

                dates.push(tempArray[0]); 

                variable1.push(tempArray[1]);

                variable2.push(tempArray[2]);

                etc... thru variable6

              }

              • 4. Re: Bit of RegEx help
                sinious Most Valuable Participant

                Probably better off making an array of arrays rather than a slew of arrays.

                 

                e.g.

                 

                var str:String = 'your content';

                 

                var splitData:Array = str.split("\r\n"); // split up data

                var data:Array = new Array(); // new data container

                 

                for (var i:int=0; i<splitData.length; i++)

                {

                   data.push(splitData[i].split(' '));

                }

                 

                Then data[0] will contain the full first set of data split up.

                 

                e.g.

                trace(data[0][0]); // 2012-03-07

                trace(data[0][1]); // 135.06

                trace(data[0][2]); // 135.91

                 

                trace(data[1][0]); // 2012-03-06

                 

                etc..

                1 person found this helpful
                • 5. Re: Bit of RegEx help
                  Ned Murphy Adobe Community Professional & MVP

                  I wouldn't say better, but that's just me.  It is an option though, but that array approach doesn't separate things the way the OP requested.

                  • 6. Re: Bit of RegEx help
                    sinious Most Valuable Participant

                    It's better because in the future if more scores are added the code to parse doesn't need to be adjusted. The 'view' part of the code can also iterate over all available indexes. Otherwise you need to literally keep making more arrays by hand to handle any variations in data.

                    • 7. Re: Bit of RegEx help
                      Ned Murphy Adobe Community Professional & MVP

                      If you change the structure of the data, you need to change things anyways, either in the parsing or in the processing. 

                       

                      Anyways, I was only indicating I wouldn't say it is better in the sense of respect/courtesy.... I try not to rank my contributions above others unless there are obvious flaws.  Mine is not flawed however, it is the same approach but is extended to provide the OP with the result requested.

                      • 8. Re: Bit of RegEx help
                        sinious Most Valuable Participant

                        Then out of respect and courtesy, why would you knock a solution that has future proofing values if the user understands how it works?

                         

                        Seriously I'm not trying to derail here any more. Your solution is fine and is what the OP originally requested. I offered an alternative that has the benefits I already mentioned. There's nothing more to say here, no need for sniping. Having been a programmer for 26 years I'm simply offering a way to handle something because in my experience, data always changes. It's better to plan ahead for it.

                        • 9. Re: Bit of RegEx help
                          Ned Murphy Adobe Community Professional & MVP

                          I wasn't knocking your solution - I was indicating I try not to play the "mine is better" game.  You were indicating yours, which is similar to mine in the approach, as being better, which would infer you were knocking mine.  Programming experience has no bearing on how you present yourself.

                          • 10. Re: Bit of RegEx help
                            sinious Most Valuable Participant

                            If the solution has obvious benfits, why knock it? The structure seems sound. Date, then a bunch of scores. That is not likely to change besides adding more scores. That is why I say parse it based upon the apparent structure of date + scores while implementing the flexability to add extra scores without any issue.