aspose file tools*
The moose likes Java in General and the fly likes Extracting numbers from a String using regular expressions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Extracting numbers from a String using regular expressions" Watch "Extracting numbers from a String using regular expressions" New topic
Author

Extracting numbers from a String using regular expressions

Nicholas van Orton
Greenhorn

Joined: Aug 13, 2005
Posts: 17
Hi all,

I'm completely new to regular expressions and I'm having a little trouble grasping all the concepts behind how to use regular expressions.

I have a String array that contains a series of weights:



At the moment, I'm adding up all the weights using a StringTokenizer to extract the numbers as follows:



I'm figuring that this is a rather inefficient way of doing things, as a new StringTokenizer object is created and subsequently destroyed on every run through the for loop.

Thus I turned to regular expressions for help. At the moment I've got:



What I'm suppose to do now is where I'm lost. I've even tried the following, to no avail.



Any help would be very much appreciated.

Thanks
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
To capture a matche using Pattern/Matcher you need to do something like this


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant."


� weight[i].substring (0, len-3); is another idea that comes to mind - consult the api-doc for the right usage of the numeric parameters (from, to).

It is another diskussion, how many objects are created behind the scene by using ([Pattern/ Matcher], [replaceAll], [StringTokenizer]).

Create a program to compare these approaches, and increase the number of weights until the results differ significantly, but study techniques for microbenchmarks first, because it's easy to do it wrong.

Or analyze the sourcecode of ([Pattern/ Matcher], [replaceAll], [StringTokenizer]).

Don't forget to post the results.
[ March 30, 2006: Message edited by: Stefan Wagner ]

http://home.arcor.de/hirnstrom/bewerbung
Nicholas van Orton
Greenhorn

Joined: Aug 13, 2005
Posts: 17
I really appreciate all of these replies, thank you.

This is definately a great place to learn more about and share ideas about Java.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Extracting numbers from a String using regular expressions