regular expression matches in an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Samji
    New Member
    • Dec 2006
    • 23

    regular expression matches in an array

    Hi, everyone. I have this code when I'm reading in a file:

    Code:
    // read lines into array
    var line = {}, lines = [], hasmore;
    do
         {
              hasmore = istream.readLine(line);
              lines.push(line.value);
         } 
         while(hasmore); 
         istream.close();
                
         // check inported file is valid by looking for <privatemessage>
         // and <fromuserid> tags using regular expressions.
                  
         var pmtag = /^\<privatemessage\>$/;
         var fuidtag = /^\<fromuserid\>$/;
    I'm try to find patterns for those two tags within my file, which has been loaded into the lines array. I have looked at regular expressions and it seems that they seem to operate on strings. So I have tried assigning the array to a string first and peforming on the exec() and match() methods on the string. However I've had no luck. I need something that returns either true or false on whether each pattern occurs somewhere in my file (the lines array).

    Thanks in advance. :)
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Do you have to match the whole file with the regular expression or line by line?

    Comment

    • Samji
      New Member
      • Dec 2006
      • 23

      #3
      It doesn't matter, as long as I can search in the file for those tags and return true or false if they occur. The code reads in my file line-by-line so that might be the preferable method. Thanks for the prompt reply. :)

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        You need to use the search() function on the string.
        Code:
        str.search(regularexpression);
        Use the i flag to make case insensitive search:
        Code:
        str.search(/string/i);

        Comment

        • Samji
          New Member
          • Dec 2006
          • 23

          #5
          Thanks I tried doing what you said.

          I tried this code:

          Code:
            // check imported file is valid by looking for "privatemessage"
            // and "fromuserid" tags (not incl. < >) using regular expressions.
               var valid = false;
               var eachline;
               for (eachline = 0; eachline < 20; eachline++ )
               {
                    var pmtag = lines[eachline].search(/privatemessage/);
                    var uitag = lines[eachline].search(/fromuserid/);
               }
          Do you know how I can make this break out of the for loop when the first instance of "privatemessage " and "fromuserid " are both found.

          I've tried a few things with no luck. Thanks again. :)

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by Samji
            Thanks I tried doing what you said.

            I tried this code:

            Code:
              // check imported file is valid by looking for "privatemessage"
              // and "fromuserid" tags (not incl. < >) using regular expressions.
                 var valid = false;
                 var eachline;
                 for (eachline = 0; eachline < 20; eachline++ )
                 {
                      var pmtag = lines[eachline].search(/privatemessage/);
                      var uitag = lines[eachline].search(/fromuserid/);
                 }
            Do you know how I can make this break out of the for loop when the first instance of "privatemessage " and "fromuserid " are both found.

            I've tried a few things with no luck. Thanks again. :)
            Use break to break out of a loop.
            Code:
            if (pmtag && uitag) break;

            Comment

            • Samji
              New Member
              • Dec 2006
              • 23

              #7
              Thanks I know have this code.

              Code:
                       
              // check imported file is valid by looking for "privatemessage"
              // and "fromuserid" tags (not incl. < >) using a regular expression.
              var eachline;
              for (eachline = 0; eachline < 11; eachline++ )
              {
                  var pmtag = lines[eachline].search(/privatemessage/);
                  var uitag = lines[eachline].search(/fromuserid/);
                              
                   // if "privatemessage" and "fromuserid" are found in file...
                   if (pmtag && uitag > -1) break; 
              }  
              // ...write master file...
              alert(":D"); //! TEST !//
              An alert box will now be displayed if the file is valid. But how can I go about making something happen if the file is not valid. I heard something about breakpoints being able to jump to labels in code?

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Originally posted by Samji
                An alert box will now be displayed if the file is valid. But how can I go about making something happen if the file is not valid. I heard something about breakpoints being able to jump to labels in code?
                Use a boolean variable. So at the beginning of your code or before you check if the file is valid, declare a variable, e.g.
                Code:
                var isValid = true;
                .
                Then during the checking, if the file is not valid (doesn't match the regular expressions), set this to false and break out of the loop.

                Outside the loop, check if the file is valid:
                Code:
                if (isValid) alert("The file is valid");
                else alert("The file is invalid!");
                or whatever you require.

                Comment

                • Samji
                  New Member
                  • Dec 2006
                  • 23

                  #9
                  Thanks again, acoder. Your help has been priceless. I have this code:

                  Code:
                  var isInvalid = true;
                  var eachline;
                  for (eachline = 0; eachline < 20; eachline++)
                  {
                     var pmtag = lines[eachline].search(/privatemessage/);
                     var uitag = lines[eachline].search(/fromuserid/);
                                  
                     // if "privatemessage" and "fromuserid" are found in file
                     if (pmtag && uitag > -1)
                     {
                         isInvalid = false;
                         break;
                     }
                  }
                  alert("isInvalid = " + isInvalid); //! TEMP !//
                  if (isInvalid == true)  // if file is found to be invalid...
                  {
                      alert("Invalid file.");
                  }
                  else 
                  {
                             .... my file code ....
                  I decided to do what you said but did a variable for when a file is invalid, so when the file is found to be valid I set isInvalid as false.

                  I put a temporary alert there to display the value and it appears when isInvalid is false. But when the isInvalid = true, my if statement is not excuted although the else part is executed when isInvalid = false. Why is this? Thanks in advance.

                  I tried having isInvalid in the if statement as just (isInvalid) and (isInvalid == true).

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    Originally posted by Samji
                    I decided to do what you said but did a variable for when a file is invalid, so when the file is found to be valid I set isInvalid as false.

                    I put a temporary alert there to display the value and it appears when isInvalid is false. But when the isInvalid = true, my if statement is not excuted although the else part is executed when isInvalid = false. Why is this? Thanks in advance.

                    I tried having isInvalid in the if statement as just (isInvalid) and (isInvalid == true).
                    Actually, having checked your code, the line
                    Code:
                    if (pmtag && uitag > -1)
                    should be
                    Code:
                    if (pmtag > -1 && uitag > -1)
                    are you trying to match only one line, i.e. if only one line matches than the whole file is valid, or do you want to match every line for the file to be valid? If it's the second one, your code is not quite correct.

                    Comment

                    • Samji
                      New Member
                      • Dec 2006
                      • 23

                      #11
                      It supposed to be if any line in the file contains either the "privatemessage " tag or the "fromuserid " tag. Thanks.

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        For that, you will still need the following line
                        Code:
                        if (pmtag > -1 && uitag > -1)

                        Comment

                        • Samji
                          New Member
                          • Dec 2006
                          • 23

                          #13
                          Originally posted by acoder
                          For that, you will still need the following line
                          Code:
                          if (pmtag > -1 && uitag > -1)
                          Thanks. I tried changing to that and it still didn't work. :(
                          I think I know why I need to break out of the loop after each line has been searched whether or not pmtag or uitag were found. How would I do this after all lines have been searched? Thanks again.

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            Originally posted by Samji
                            Thanks. I tried changing to that and it still didn't work. :(
                            I think I know why I need to break out of the loop after each line has been searched whether or not pmtag or uitag were found. How would I do this after all lines have been searched? Thanks again.
                            After all lines have been searched, it will break out of the loop of its own accord, assuming you are looping over all the lines in the first place.

                            Comment

                            • acoder
                              Recognized Expert MVP
                              • Nov 2006
                              • 16032

                              #15
                              Ok, let's look at your code so far:
                              Code:
                              var isInvalid = true;
                              var eachline;
                              for (eachline = 0; eachline < 20; eachline++)
                              {
                                 var pmtag = lines[eachline].search(/privatemessage/);
                                 var uitag = lines[eachline].search(/fromuserid/);
                                              
                                 // if "privatemessage" and "fromuserid" are found in file
                                 if (pmtag > -1 && uitag > -1)
                                 {
                                     isInvalid = false;
                                     break;
                                 }
                              }
                              alert("isInvalid = " + isInvalid); //! TEMP !//
                              if (isInvalid)  // if file is found to be invalid...
                              {
                                  alert("Invalid file.");
                              }
                              else 
                              {
                                         .... my file code ....
                              Obviously, you will use the length of the lines array instead of 20, once testing is over.

                              I tested with some slight modifications, and it seems to work fine for me, for both valid and invalid data.

                              I tested with a single line that first contained
                              "privatemes sage fromuserid" and then
                              "privatemes sae fromuserid" (just one letter missing). I know this is not an exact situation but will do for testing. It worked fine in both instances.

                              Comment

                              Working...