0

I'm wondering if there is a java library, which can parse strings in a similar manner as date parsers. For example with the SimpleDateFormat class it is possible to define a pattern (yyMMddHHmmssZ) to parse a String (010704120856-0700) for getting the date. Instead of parsing dates I would like to parse arbitary numbers.

For example I would like to be able to parse:

String:  X0759502.CSV
Pattern: DSSIYYMM.CSV   
   X -> Data version:
         'X' GOES X-ray
         'M' GOES Magnetometer
         'P' GOES Electrons, Protons & Alpha particles.
   07 -> GOES-7, 91 = SMS-1, 92=SMS-2, etc.
    3 -> 3-second values
   YY -> year
   MM -> month

And then for example getting a hashmap, where I can use the key word (ex. YY) to get the extracted value.

JoLau
  • 15
  • 7

2 Answers2

1

it appears that you want regular expression parsing (aka regex). Check out Matcher and Pattern. The links are to the java 7 API, but both classes have been around since java 1.5 (or before).

DwB
  • 37,124
  • 11
  • 56
  • 82
0

With java it is possible to use Regex's Named Groups. With that approache it is possible to achieve something similar to a SimpleDateFormat parser.

To parse the provided example (X0759502.CSV), this regex was used:

x(?<satellite>\\d{2})(?<interval>\\d)(?<year>\\d{2})(?<month>\\d{2}).csv$

With the matcher.group("satellite") is used to get the 07.

Community
  • 1
  • 1
JoLau
  • 15
  • 7