• 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
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Looking for a better approach in my regEx design.

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I have a text data in which I need to extract the taxes (in the data below it's JC = 5.20, FS = 17.60). Those taxes change from state to state so it's not always JC, FS etc; and sometimes they don't even exists (system error) or there's only one tax (system error)
My task is to extract those taxes into a map<String,Double). One approach I thought of is to use matcher and patter and use in the compile: \s\w\w\s+\d+\.\d\d
I was wondering if there's a better approach?
Thanks!


 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That regex already looks pretty good. I'd add just two improvements:
1) use {X} to check for exactly X occurrences.
2) use grouping for both the code and the number. You can then more easily retrieve the values.

Your regex becomes "\\s(\\w{2})\\s+(\\d+\\.\\d{2})". You can then use matcher.group(1) to get the first group (the "(\\w{2})") and matcher.group(2) to get the second group (the "(\\d+\\.\\d{2})"). That way you no longer need to use substring / indexOf to get these values.
 
Adrian Burlington
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!
 
Rob Spoor
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
He was giving me directions and I was powerless to resist. I cannot resist this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic