Win a copy of Modern frontends with htmx this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Extracting numbers from a String using regular expressions

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To capture a matche using Pattern/Matcher you need to do something like this
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic