• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic