I have multiple strings that are in the following format:
12/18/2009 02:08:26 Admitted Doe, John (Card #111) at South Lobby [In]
From these string I need to get out the date, time, first and last name of the person, and the card number. The word admitted can be omitted and anything following the final digit of the card number can be ignored.
I have a feeling I want to use StringTokenizer for this, but I’m not positive.
Any suggestions?
Your record format is simple enough that I’d just use String’s split method to get the date and time. As pointed out in the comments, having names that can contain spaces complicates things just enough that splitting the record by spaces won’t work for every field. I used a regular expression to grab the other three pieces of information.
The regular expression
"Admitted (.*), (.*) \\(Card #(.*)\\)"uses grouping parentheses to store the information you’re trying to extract. The parentheses that exist in your record must be escaped.Running the code above gives me the following output: