Hi, everyone. I have this code when I'm reading in a file: -
// 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. :)
20 1996
Do you have to match the whole file with the regular expression or line by line?
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. :)
You need to use the search() function on the string. - str.search(regularexpression);
Use the i flag to make case insensitive search:
Thanks I tried doing what you said.
I tried this 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. :)
Thanks I tried doing what you said.
I tried this 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. - if (pmtag && uitag) break;
Thanks I know have this 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?
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.
.
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: - if (isValid) alert("The file is valid");
-
else alert("The file is invalid!");
or whatever you require.
Thanks again, acoder. Your help has been priceless. I have this 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).
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
should be - 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.
It supposed to be if any line in the file contains either the "privatemessage" tag or the "fromuserid" tag. Thanks.
For that, you will still need the following line - if (pmtag > -1 && uitag > -1)
For that, you will still need the following line - 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.
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.
Ok, let's look at your code so far: -
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
"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.
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. - "<?xml".search(/\<\?xml/);
For some background, the file I have to read is something like this. Bold are the tags I want to find a match for. -
<privatemessages>
-
<folder name="Sent Items">
-
<privatemessage>
-
<datestamp>2006-06-08 18:47</datestamp>
-
<title>Red hyperlinks with CSS?</title>
-
<fromuser>Tommy</fromuser>
-
<fromuserid>2420</fromuserid>
-
<touser>Sam</touser>
-
<message>Some message</message>
-
</privatemessage>
-
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. - "<?xml".search(/\<\?xml/);
See this site for info. on regular expressions.
For some background, the file I have to read is something like this. Bold are the tags I want to find a match for. -
<privatemessages>
-
<folder name="Sent Items">
-
<privatemessage>
-
<datestamp>2006-06-08 18:47</datestamp>
-
<title>Red hyperlinks with CSS?</title>
-
<fromuser>Tommy</fromuser>
-
<fromuserid>2420</fromuserid>
-
<touser>Sam</touser>
-
<message>Some message</message>
-
</privatemessage>
-
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 || - if (pmtag > -1 || uitag > -1)
Thanks for your help. Changing that solved it. :)
Thanks for your help. Changing that solved it. :)
You're welcome. Glad I could be of help.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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:
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |