• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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: 22701
129
Eclipse IDE Spring VI Editor 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: 22701
129
Eclipse IDE Spring VI Editor 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
 
Rototillers convert rich soil into dirt. Please note that this tiny ad is not a rototiller:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic