0

I want to extract the date from the following sentence in java.

"D759,301 Elhalwani June 14, 2016 Hookah Claims CLAIM The ornamental design for a hookah, as shown and described"

Given that there are other sentences with the same structure but different dates, that I also want to extract the date from them.

Jeremy Hunts
  • 353
  • 1
  • 5
  • 14
  • Use Regular Expression with Match Groups: http://stackoverflow.com/questions/17969436/java-regex-capturing-groups – Benjamin M Jun 19 '16 at 23:37
  • Do you only have to worry about this message format (dates always in the same spot)? Do you only have to worry about this date format? If message formats can differ, do you need to worry about the being multiple dates? – Clockwork-Muse Jun 20 '16 at 00:08

1 Answers1

1

1- record input in a String

2- split(" ") String in an array --> without the space

Now if the date is always at spot #2,3 and 4 in the array, it's easy to just look for them. But If they are not always at the same place, then you should create an array "month", "date" and "year" and compare each index of the original array. If they are the same --> record date, else continue.

Mrtnchps
  • 187
  • 1
  • 11
  • 1
    Wait, what? How is that supposed to help if the date parts end up in a different location? Perhaps you could supply a more concrete example? What happens if somebody uses a different date format? – Clockwork-Muse Jun 20 '16 at 00:06
  • It depends on what you are looking for. If you are looking for all the date format possible, then the code get much more complex. – Mrtnchps Jun 20 '16 at 00:19
  • ...what I'm getting at is, you claim that it's easy to find the date if it's not at the same spot - how do you anticipate this being accomplished? And what problems might you run into with a naïve scheme? – Clockwork-Muse Jun 20 '16 at 01:58
  • String[] input = {"I","am", "2016", "hello" }; and String[] years ={"2014", "2015", "2016"} --> you compare in a loop each index of the two arrays. input[2] match years[2], so you record that, etc... – Mrtnchps Jun 20 '16 at 02:06
  • ...would fail on sample data for day-of-month (split values will include comma). Relies overly much on no other data matching one of the values (may result in unrelated pieces of data being picked up). Requires knowledge of all possible values, although that's not too much of a problem. Automatic n² algorithm. If the format is known to always be in this format, but not _where_, a regex capturing group is likely to work better. – Clockwork-Muse Jun 20 '16 at 04:07
  • It really depends on what you are looking for. I assumed the question about the date was only about the format described in the question. – Mrtnchps Jun 20 '16 at 04:53