473,434 Members | 1,732 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,434 software developers and data experts.

regular expression help...

I'm looking to use Javascript to pull apart a page of HTML I have
already fetched. The page contains a table, within which there are rows
containing...

Either:

0000000 - 0000000<some html>00<some html>0

or:

0000000 - 0000000<some html>00 - 00<some html>0

I'm interested in extracting only the numbers (which may not always be
0!), in each case. I bet this can be done using a regular expression (or
two). Can anyone help?

Thanks,

Ian
Jul 23 '05 #1
5 1462
A possible way, not necessarily the best but a way is:

<script>
var foo="0000000 - 0000000<some html>00<some html>0"
alert(foo.match(/\d+/g))
</script>

You can add properties to the script tag if you prefer, not so important.

I hope that was close to what you may need.
Note that you'd first check to be safer if match actually returned an array
or, if no match, null.

Maybe better solutions will follow. Mine is just one.

ciao
Alberto
http://www.unitedscripters.com/

"Ian Richardson" <za*****@chaos.org.uk> ha scritto nel messaggio
news:2o************@uni-berlin.de...
I'm looking to use Javascript to pull apart a page of HTML I have
already fetched. The page contains a table, within which there are rows
containing...

Either:

0000000 - 0000000<some html>00<some html>0

or:

0000000 - 0000000<some html>00 - 00<some html>0

I'm interested in extracting only the numbers (which may not always be
0!), in each case. I bet this can be done using a regular expression (or
two). Can anyone help?

Thanks,

Ian

Jul 23 '05 #2
Ian Richardson wrote:
Either:

0000000 - 0000000<some html>00<some html>0

or:

0000000 - 0000000<some html>00 - 00<some html>0

I'm interested in extracting only the numbers (which may not always be
0!), in each case. I bet this can be done using a regular expression (or
two). Can anyone help?

Thanks,

Ian

You don't necessarily need to use regex, you can use the DOM methods,
for example (without error checking):

var tds=document.getElementsByTagName("TD")

//Then loop through the collection
var numbers=[];
for(var i=0;i<tds.length;i++){
if(!isNaN(tds.item(i).firstChild.data)){
numbers.push(tds.item(i).firstChild.data)
}
}

I'm not sure that the method above is the best at identifying Numbers,
and it will fail if you use tags within the <td> tag, but it is
something to get you thinking.
Mick

Jul 23 '05 #3
Mick White wrote:
Ian Richardson wrote:
Either:

0000000 - 0000000<some html>00<some html>0

or:

0000000 - 0000000<some html>00 - 00<some html>0

I'm interested in extracting only the numbers (which may not always be
0!), in each case. I bet this can be done using a regular expression
(or two). Can anyone help?

Thanks,

Ian


You don't necessarily need to use regex, you can use the DOM methods,
for example (without error checking):


I can't use DOM methods if all I'm dealing with is one long string of
HTML which contains the numeric data I wish to extract...

I can't just search through the string looking for numeric data as I'm
likely to pick up other stuff which I don't need.

I was really hoping for a regular expression to do extract multiples of:

7 digits and 7 digits separated by " - "
(HTML I'm not interested in)
Either 2 digits, or 2 digits followed by " - " and another 2 digits
(HTML I'm not interested in)
1 digit

....from within an HTML table, the markup of which can change...

Any other ideas?

Thanks,

Ian
Jul 23 '05 #4
On Thu, 19 Aug 2004 15:53:28 +0100, Ian Richardson <za*****@chaos.org.uk>
wrote:

[snip]
I was really hoping for a regular expression to do extract multiples of:

7 digits and 7 digits separated by " - "
(HTML I'm not interested in)
Either 2 digits, or 2 digits followed by " - " and another 2 digits
(HTML I'm not interested in)
1 digit

...from within an HTML table, the markup of which can change...


Alberto's suggestion is a good start, but it depends on whether the
mark-up can contain numbers itself. If it can, there needs to be more
structure in the expression. Try:

/(\d{7}) - (\d{7})\x3c.+\x3e(\d{2})( - (\d{2}))?\x3c.+\x3e(\d)/

When used with the exec() method, it will return an array that contains:

element 0 - ignore
1 - first group of seven digits
2 - second group
3 - first group of two digits
4 - ignore
5 - second group of two digits
(undefined if they don't exist)
6 - final digit

This appears to be safe, even if numbers appear in the separating HTML,
but without live data, I can't test properly.

Give it a try. If it doesn't work and you can't modify the expression,
show the exact test case you used and we will see what we can do.

Good luck,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #5
Ian Richardson <za*****@chaos.org.uk> wrote in message news:<2o************@uni-berlin.de>...
I'm looking to use Javascript to pull apart a page of HTML I have
already fetched. The page contains a table, within which there are rows
containing...

Either:

0000000 - 0000000<some html>00<some html>0

or:

0000000 - 0000000<some html>00 - 00<some html>0

<snip>
Try this:

<script type="text/javascript">
var reStrip = new RegExp("([0-9]{7}) -
([0-9]{7})<[^>]+>(([0-9]{2})( - ([0-9]{2}))?)<[^>]+>([0-9]{1})",
'gi');

var strTest = '';

strTest = '0000000 - 0000000<some html>00<some html>0';
if (strTest.match(reStrip)){

alert('Match!');

}else{

alert('No Match!');

}


// The first number will be returned as $1, as in:
// strTest.replace(reStrip, '$1')
//
// Just be aware that in the case of the second set of numbers (
// ('00 - 00' instead of '00', you will have additional values
(one for each
// matching set of parethesis..
strTest = '0000000 - 0000000<some html>00 - 00<some html>0';
if (strTest.match(reStrip)){

alert('Match!');

}else{

alert('No Match!');

}

</script>
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bradley Plett | last post by:
I'm hopeless at regular expressions (I just don't use them often enough to gain/maintain knowledge), but I need one now and am looking for help. I need to parse through a document to find a URL,...
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...
10
by: Lee Kuhn | last post by:
I am trying the create a regular expression that will essentially match characters in the middle of a fixed-length string. The string may be any characters, but will always be the same length. In...
3
by: James D. Marshall | last post by:
The issue at hand, I believe is my comprehension of using regular expression, specially to assist in replacing the expression with other text. using regular expression (\s*) my understanding is...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
9
by: Pete Davis | last post by:
I'm using regular expressions to extract some data and some links from some web pages. I download the page and then I want to get a list of certain links. For building regular expressions, I use...
3
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular...
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...
3
by: Mr.Steskal | last post by:
Posted: Wed Jul 11, 2007 7:01 am Post subject: Regular Expression Help -------------------------------------------------------------------------------- I need help writing a regular...
18
by: Lit | last post by:
Hi, I am looking for a Regular expression for a password for my RegExp ValidationControl Requirements are, At least 8 characters long. At least one digit At least one upper case character
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.