Reputation: 1165
I have a multi-line String, something like this:
Welcome to blah blah blah. Below is the information you requested:
temperature: 12c
windspeed: 30 knotts
humidity: 29%
I hope you have a nice day
ok this is a simplified version. Anyway I want to get the value of a field (e.g. windspeed). This is one string with new lines (as an aside sometimes strings are just separated by \n and sometimes by \r\n and I am not sure which, but that is an aside).
Anyway, someone wrote this method
private String getField(String pdfContent, String field) {
String temp;
String value = null;
int idx = pdfContent.indexOf(field);
if (idx != -1) {
temp = pdfContent.substring(idx);
String line = temp.split(":")[1];
value = line.split("\n")[0].trim();
l.info(field + value);
}
return value;
}
Which is fine except say in the instance below, if you want to find value of name:
You requested name, age and telephone number. Below are the results
name: Jenny
age: 22
telephone: 867-5309
the method will find the first instance of name. I guess I could look for a colon after the field but there could be a colon after that as in
Following is your request for telephone and name:
telephone: 867-5309
name: Jenny
In a normal regexp I would look for "^name:" but I don't think that works in this instance. Is there a way to find the field at the beginning of a line (in one String)? Best to look for the colon too, as you could have
name: Merrill Lynch Pierce Fenner Smith
name_common: Merrill Lynch
just looking for name would find the Merrill Lynch Pierce Fenner Smith first
Oh and yes, this is Java
Upvotes: 1
Views: 564
Reputation: 757
you could use: /^name:\s(.*)$/gm
with ^
you ensure the line starts with the field.
:\s
make sure there is a semicolin and a space after the field.
(.*)$
will read the rest of the line and place it into a group.
Upvotes: 0
Reputation: 299
As you have a multi line string i would reckon you to use the Scanner class findinLine() method, this method will read the parts of your string as:
scannerobj.findInLine("temperature:"); O/p: 12c
scannerobj.findInLine("windspeed:"); O/p: 30 knotts
scannerobj.findInLine("humidity:"); O/p: 29%
Upvotes: 2