File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extracting numbers from a String using regular expressions

 
Nicholas van Orton
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 262
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To capture a matche using Pattern/Matcher you need to do something like this
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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 ]
 
Nicholas van Orton
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic