473,396 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

regular expression matches in an array

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

Expand|Select|Wrap|Line Numbers
  1. // read lines into array
  2. var line = {}, lines = [], hasmore;
  3. do
  4.      {
  5.           hasmore = istream.readLine(line);
  6.           lines.push(line.value);
  7.      } 
  8.      while(hasmore); 
  9.      istream.close();
  10.  
  11.      // check inported file is valid by looking for <privatemessage>
  12.      // and <fromuserid> tags using regular expressions.
  13.  
  14.      var pmtag = /^\<privatemessage\>$/;
  15.      var fuidtag = /^\<fromuserid\>$/;
  16.  
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. :)
Jan 13 '07 #1
20 1996
acoder
16,027 Expert Mod 8TB
Do you have to match the whole file with the regular expression or line by line?
Jan 13 '07 #2
Samji
23
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. :)
Jan 13 '07 #3
acoder
16,027 Expert Mod 8TB
You need to use the search() function on the string.
Expand|Select|Wrap|Line Numbers
  1. str.search(regularexpression);
Use the i flag to make case insensitive search:
Expand|Select|Wrap|Line Numbers
  1. str.search(/string/i);
Jan 13 '07 #4
Samji
23
Thanks I tried doing what you said.

I tried this code:

Expand|Select|Wrap|Line Numbers
  1.   // check imported file is valid by looking for "privatemessage"
  2.   // and "fromuserid" tags (not incl. < >) using regular expressions.
  3.      var valid = false;
  4.      var eachline;
  5.      for (eachline = 0; eachline < 20; eachline++ )
  6.      {
  7.           var pmtag = lines[eachline].search(/privatemessage/);
  8.           var uitag = lines[eachline].search(/fromuserid/);
  9.      }
  10.  
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. :)
Jan 13 '07 #5
acoder
16,027 Expert Mod 8TB
Thanks I tried doing what you said.

I tried this code:

Expand|Select|Wrap|Line Numbers
  1.   // check imported file is valid by looking for "privatemessage"
  2.   // and "fromuserid" tags (not incl. < >) using regular expressions.
  3.      var valid = false;
  4.      var eachline;
  5.      for (eachline = 0; eachline < 20; eachline++ )
  6.      {
  7.           var pmtag = lines[eachline].search(/privatemessage/);
  8.           var uitag = lines[eachline].search(/fromuserid/);
  9.      }
  10.  
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.
Expand|Select|Wrap|Line Numbers
  1. if (pmtag && uitag) break;
Jan 13 '07 #6
Samji
23
Thanks I know have this code.

Expand|Select|Wrap|Line Numbers
  1.  
  2. // check imported file is valid by looking for "privatemessage"
  3. // and "fromuserid" tags (not incl. < >) using a regular expression.
  4. var eachline;
  5. for (eachline = 0; eachline < 11; eachline++ )
  6. {
  7.     var pmtag = lines[eachline].search(/privatemessage/);
  8.     var uitag = lines[eachline].search(/fromuserid/);
  9.  
  10.      // if "privatemessage" and "fromuserid" are found in file...
  11.      if (pmtag && uitag > -1) break; 
  12. }  
  13. // ...write master file...
  14. alert(":D"); //! TEST !//
  15.  
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?
Jan 14 '07 #7
acoder
16,027 Expert Mod 8TB
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.
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. if (isValid) alert("The file is valid");
  2. else alert("The file is invalid!");
or whatever you require.
Jan 15 '07 #8
Samji
23
Thanks again, acoder. Your help has been priceless. I have this code:

Expand|Select|Wrap|Line Numbers
  1. var isInvalid = true;
  2. var eachline;
  3. for (eachline = 0; eachline < 20; eachline++)
  4. {
  5.    var pmtag = lines[eachline].search(/privatemessage/);
  6.    var uitag = lines[eachline].search(/fromuserid/);
  7.  
  8.    // if "privatemessage" and "fromuserid" are found in file
  9.    if (pmtag && uitag > -1)
  10.    {
  11.        isInvalid = false;
  12.        break;
  13.    }
  14. }
  15. alert("isInvalid = " + isInvalid); //! TEMP !//
  16. if (isInvalid == true)  // if file is found to be invalid...
  17. {
  18.     alert("Invalid file.");
  19. }
  20. else 
  21. {
  22.            .... my file code ....
  23.  
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).
Jan 15 '07 #9
acoder
16,027 Expert Mod 8TB
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
Expand|Select|Wrap|Line Numbers
  1. if (pmtag && uitag > -1)
should be
Expand|Select|Wrap|Line Numbers
  1. 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.
Jan 15 '07 #10
Samji
23
It supposed to be if any line in the file contains either the "privatemessage" tag or the "fromuserid" tag. Thanks.
Jan 15 '07 #11
acoder
16,027 Expert Mod 8TB
For that, you will still need the following line
Expand|Select|Wrap|Line Numbers
  1. if (pmtag > -1 && uitag > -1)
Jan 15 '07 #12
Samji
23
For that, you will still need the following line
Expand|Select|Wrap|Line Numbers
  1. 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.
Jan 15 '07 #13
acoder
16,027 Expert Mod 8TB
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.
Jan 15 '07 #14
acoder
16,027 Expert Mod 8TB
Ok, let's look at your code so far:
Expand|Select|Wrap|Line Numbers
  1. var isInvalid = true;
  2. var eachline;
  3. for (eachline = 0; eachline < 20; eachline++)
  4. {
  5.    var pmtag = lines[eachline].search(/privatemessage/);
  6.    var uitag = lines[eachline].search(/fromuserid/);
  7.  
  8.    // if "privatemessage" and "fromuserid" are found in file
  9.    if (pmtag > -1 && uitag > -1)
  10.    {
  11.        isInvalid = false;
  12.        break;
  13.    }
  14. }
  15. alert("isInvalid = " + isInvalid); //! TEMP !//
  16. if (isInvalid)  // if file is found to be invalid...
  17. {
  18.     alert("Invalid file.");
  19. }
  20. else 
  21. {
  22.            .... my file code ....
  23.  
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
"privatemessage fromuserid" and then
"privatemessae fromuserid" (just one letter missing). I know this is not an exact situation but will do for testing. It worked fine in both instances.
Jan 15 '07 #15
Samji
23
Thanks again for your help.

I changed the code like you said (e.g. used lines.length), but I can't get the validation to work correctly. Do you think it is something to do with my for loop. What changes did you make exactly that worked for you?

Additionally, is it possible to use full blown regular expressions in the search method such as looking for exact matches to words or those that contain symbols such as question marks. e.g.

Expand|Select|Wrap|Line Numbers
  1. "<?xml".search(/\<\?xml/);
Jan 19 '07 #16
Samji
23
For some background, the file I have to read is something like this. Bold are the tags I want to find a match for.

Expand|Select|Wrap|Line Numbers
  1. <privatemessages>
  2.     <folder name="Sent Items">
  3.            <privatemessage>
  4.             <datestamp>2006-06-08 18:47</datestamp>
  5.             <title>Red hyperlinks with CSS?</title>
  6.             <fromuser>Tommy</fromuser>
  7.             <fromuserid>2420</fromuserid>
  8.             <touser>Sam</touser>
  9.             <message>Some message</message>
  10.         </privatemessage>
  11.  
Jan 19 '07 #17
acoder
16,027 Expert Mod 8TB
Thanks again for your help.

I changed the code like you said (e.g. used lines.length), but I can't get the validation to work correctly. Do you think it is something to do with my for loop. What changes did you make exactly that worked for you?

Additionally, is it possible to use full blown regular expressions in the search method such as looking for exact matches to words or those that contain symbols such as question marks. e.g.

Expand|Select|Wrap|Line Numbers
  1. "<?xml".search(/\<\?xml/);
See this site for info. on regular expressions.
Jan 19 '07 #18
acoder
16,027 Expert Mod 8TB
For some background, the file I have to read is something like this. Bold are the tags I want to find a match for.

Expand|Select|Wrap|Line Numbers
  1. <privatemessages>
  2.     <folder name="Sent Items">
  3.            <privatemessage>
  4.             <datestamp>2006-06-08 18:47</datestamp>
  5.             <title>Red hyperlinks with CSS?</title>
  6.             <fromuser>Tommy</fromuser>
  7.             <fromuserid>2420</fromuserid>
  8.             <touser>Sam</touser>
  9.             <message>Some message</message>
  10.         </privatemessage>
  11.  
At the moment, in your code, you are searching for both privatemessage and fromuserid in one line. From this file, it seems you need to search for either (not both) on a line. So replace && with ||
Expand|Select|Wrap|Line Numbers
  1. if (pmtag > -1 || uitag > -1)
Jan 19 '07 #19
Samji
23
Thanks for your help. Changing that solved it. :)
Jan 20 '07 #20
acoder
16,027 Expert Mod 8TB
Thanks for your help. Changing that solved it. :)
You're welcome. Glad I could be of help.
Jan 20 '07 #21

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
2
by: Bryce Budd | last post by:
Hi all, I am trying to use a regular expression validator to check for the existence of PO Box in an address textbox. The business rule is "No addresses with PO Boxes are allowed." What I...
11
by: Martin Robins | last post by:
I am trying to parse a string that is similar in form to an OLEDB connection string using regular expressions; in principle it is working, but certain character combinations in the string being...
4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
11
by: Dimitris Georgakopuolos | last post by:
Hello, I have a text file that I load up to a string. The text includes certain expression like {firstName} or {userName} that I want to match and then replace with a new expression. However,...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
4
by: mosesdinakaran | last post by:
Can any one explain how the rule is applied for the following Regular expression $Str = 'the red king'; $Pattern = '/((red|white) (king|queen))/'; preg_match($Pattern,$Str,$Val); Result:
5
by: mikko.n | last post by:
I have recently been experimenting with GNU C library regular expression functions and noticed a problem with pattern matching. It seems to recognize only the first match but ignoring the rest of...
14
by: Andy B | last post by:
I need to create a regular expression that will match a 5 digit number, a space and then anything up to but not including the next closing html tag. Here is an example: <startTag>55555 any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.