12 Replies Latest reply on Nov 28, 2009 2:42 AM by dgwillia1982

    Assigning result of a match to a variable

    dgwillia1982

      I am trying to assign the result of a match (regular expression) to a variable. The code below throws an error:

       

      urlManipulation = titleString.match (/(((http|ftp|https):\/\/){1}([a-zA-Z0-9_-]+)(\.[a-zA-Z0-9_-]+)+([\S,:\/\.\?=a-zA-Z0-9_-] +))/igs));

       

      Compiler says it was expecting right brace before right paren

       

      I am pretty sure there is nothing wrong with the reg expression but instead the syntax of the assignment I am trying to perform

       

      Any ideas?

        • 1. Re: Assigning result of a match to a variable
          Subeesh Arakkan Level 4

          Hi,

           

          Try this

           

          urlManipulation = titleString.match(new RegExp("/(((http|ftp|https):\/\/){1}([a-zA-Z0-9_-]+)(\.[a-zA-Z0-9_-]+)+([\S,:\/\.\?=a- zA-Z0-9_-]+))/","igs"));

          • 2. Re: Assigning result of a match to a variable
            dgwillia1982 Level 1

            Thanks for that.

             

            However, I know get the following error...

             

            Implicit Coercion of a value of type Array to an unrelated type String.

             

            However both variables have been defined as Strings so I dont understand this. I have pasted more code below

             

            [Bindable]
                        private var resultArrayCollection:ArrayCollection;
                       
                        private var titleString:String; 
                   
                        private var urlManipulation:String;
                       
                        private function onResult (e:ResultEvent):void   
                        {   
                            resultArrayCollection = e.result.feed.entry as ArrayCollection;
                           
                            for(var count:int = 0; count < resultArrayCollection.length; count++)
                            {
                                titleString = resultArrayCollection[count].title;
                                urlManipulation = titleString.match(new RegExp("/(((http|ftp|https):\/\/){1}([a-zA-Z0-9_-]+)(\.[a-zA-Z0-9_-]+)+([\S,:\/ \.\?=a- zA-Z0-9_-]+))/","igs"));
                                trace(urlManipulation);
                                trace(titleString);
                               
                               
                            }
                           
                        }
                       
                    ]]>


                </mx:Script>

             

             

            Can you help?

            • 3. Re: Assigning result of a match to a variable
              Subeesh Arakkan Level 4

              Hi,

               

              String.match() returns you an array of strings consisting of all substrings in   the string that match the specified pattern.

               

              urlManipulation should be an array.You can check the length of the array and access the string like

               

              urlManipulation[ 0 ] ( first item in the array )

               

              Check this page for more info

               

              http://livedocs.adobe.com/flex/3/langref/String.html#match%28%29

              1 person found this helpful
              • 4. Re: Assigning result of a match to a variable
                dgwillia1982 Level 1

                Thanks, I think I am getting there.

                 

                One other problem now though, the array length is 0, and I know it should contain matches. When I trace the array it comes back with Undefined. Also the Length when I debug is 0. My updated code is below:

                 

                <mx:Script>
                        <![CDATA[
                            import mx.collections.ArrayCollection;
                            import mx.rpc.events.ResultEvent;
                           
                            [Bindable]
                            private var resultArrayCollection:ArrayCollection;
                           
                            private var titleString:String; 
                       
                            private var urlManipulation:Array;
                           
                            private function onResult (e:ResultEvent):void   
                            {   
                                resultArrayCollection = e.result.feed.entry as ArrayCollection;
                               
                                for(var count:int = 0; count < resultArrayCollection.length; count++)
                                {
                                    titleString = resultArrayCollection[count].title;
                                    urlManipulation = titleString.match(new RegExp("/(((http|ftp|https):\/\/){1}([a-zA-Z0-9_-]+)(\.[a-zA-Z0-9_-]+)+([\S,:\/ \.\?=a- zA-Z0-9_-]+))/","igs"));
                                    trace(urlManipulation[0]);
                                    trace(titleString);
                                   
                                   
                                }
                               
                            }
                           
                        ]]>
                    </mx:Script>

                Does anything stand out as being wrong?

                • 5. Re: Assigning result of a match to a variable
                  Subeesh Arakkan Level 4

                  Hi,

                   

                  It could be the forward slashes in the regex or the flags ( igs )

                   

                  check with this

                   

                  match(new RegExp("(((http|ftp|https):\/\/){1}([a-zA-Z0-9_-]+)(\.[a-zA-Z0-9_-]+)+([\S,:\/\.\?=a- zA-Z0-9_-]+))","igs"));

                  • 6. Re: Assigning result of a match to a variable
                    Subeesh Arakkan Level 4

                    This worked for me

                     

                     

                    var str:String = "http://www.google.com";

                    var arr:Array = str.match(new RegExp("(((http|ftp|https):\/\/){1}([a-zA-Z0-9_-]+)(\.[a-zA-Z0-9_-]+)+([\S,:\/\.\?=a-zA-Z 0-9_-]+))","igs"));

                    • 7. Re: Assigning result of a match to a variable
                      dgwillia1982 Level 1

                      Thanks

                       

                      This did solve the undefined problem, but now the Regular expression you provided, does not stop at whitespace.

                       

                      For instance it brings back results such as

                       

                      - http://google.com this is a great site

                       

                      rather thanjust

                       

                      - http://google.com

                       

                      Do you know what changes I can make to the Regular expression to make sure it only brings back the URL? Logically I would like it to search fro 'http' then bring back any characters after that up until a space.

                       

                      any ideas?

                      • 8. Re: Assigning result of a match to a variable
                        Subeesh Arakkan Level 4

                        This will work for you

                         

                        var str:String = "http://www.google.com cvcvcv";
                        var arr:Array = str.match(new RegExp("^((ht|f)tp(s?)\:\/\/|~/|/)?([w]{2}([\w\-]+\.)+([\w]{2,5}))?(:[\d]{1,5})?+([a-zA-Z 0-9\/\_\?\.\,\&]+)?","igs"))

                         

                        Also check this link.You will find good regex examples here

                        http://ryanswanson.com/regexp/#start

                        1 person found this helpful
                        • 9. Re: Assigning result of a match to a variable
                          dgwillia1982 Level 1

                          Thanks again for your help, unfortunately the reg expression you provided above results in the undefined error again.

                           

                          I have pasted the latest code below, in case there is anything else you can think of?

                           

                          <mx:Script>
                                  <![CDATA[
                                      import mx.collections.ArrayCollection;
                                      import mx.rpc.events.ResultEvent;
                                     
                                      [Bindable]
                                      private var resultArrayCollection:ArrayCollection;
                                     
                                      private var titleString:String; 
                                     
                                      private function onResult (e:ResultEvent):void   
                                      {   
                                          resultArrayCollection = e.result.feed.entry as ArrayCollection;
                                         
                                          for(var count:int = 0; count < resultArrayCollection.length; count++)
                                          {
                                              titleString = resultArrayCollection[count].title;
                                              var arr:Array = titleString.match(new RegExp("^((ht|f)tp(s?)\:\/\/|~/|/)?([w]{2}([\w\-]+\.)+([\w]{2,5}))?(:[\d]{1,5}) ?+([a-zA-Z0-9\/\_\?\.\,\&]+)?","igs"));
                                             
                                              trace(arr[0]);
                                              trace(titleString);
                                             
                                          }
                                         
                                      }
                                     
                                  ]]>
                              </mx:Script>

                          • 10. Re: Assigning result of a match to a variable
                            Subeesh Arakkan Level 4

                            there should not be any white space inside the regular expression.

                             

                            match(new RegExp("^((ht|f)tp(s?)\:\/\/|~/|/)?([w]{2}([\w\-]+\.)+([\w]{2,5}))?(:[\d]{1,5})?+([a-zA-Z0-9\/\_\?\.\,\&]+)?","igs"))
                            
                            • 11. Re: Assigning result of a match to a variable
                              dgwillia1982 Level 1

                              the above reg exp just brings back the first word within the string now.

                               

                              <mx:Script>
                                      <![CDATA[
                                          import mx.collections.ArrayCollection;
                                          import mx.rpc.events.ResultEvent;
                                         
                                          [Bindable]
                                          private var resultArrayCollection:ArrayCollection;
                                         
                                          private var titleString:String; 
                                         
                                          private function onResult (e:ResultEvent):void   
                                          {   
                                              resultArrayCollection = e.result.feed.entry as ArrayCollection;
                                             
                                              for(var count:int = 0; count < resultArrayCollection.length; count++)
                                              {
                                                  titleString = resultArrayCollection[count].title;
                                                  var arr:Array = titleString.match(new RegExp("^((ht|f)tp(s?)\:\/\/|~/|/)?([w]{2}([\w\-]+\.)+([\w]{2,5}))?(:[\d]{1,5})?+([a-zA-Z 0-9\/\_\?\.\,\&]+)?","igs"));
                                                 
                                                  trace(arr[0]);
                                                  trace(titleString);
                                                 
                                              }
                                             
                                          }
                                         
                                      ]]>
                                  </mx:Script>

                              • 12. Re: Assigning result of a match to a variable
                                dgwillia1982 Level 1

                                looks like problem solvec with following reg expression...

                                 

                                var arr:Array = titleString.match(new RegExp(/(((http|ftp|https):\/\/){1}([a-zA-Z0-9_-]+)(\.[a-zA-Z0-9_-]+)+([\S,:\/\.\?=a-zA-Z 0-9_-]+))/igs));