(?m)^truck(?:(?:\r\n|[\r\n]).+$)*
This assumes the whole text has been read into a single string (i.e., you're not reading a file line-by-line), but it doesn't assume the line separator is always \n, as your code does. At the minimum you should allow for \r\n and \r as well, which is what (?:\r\n|[\r\n]) does. But it still matches only one separator, so the match stops before the double line separator at the end of the block.
Once you've matched a block of data, you can split it on the line separators to get the individual lines. Here's an example:
Pattern p0 = Pattern.compile("(?m)^truck(?:(?:\r\n|[\r\n]).+$)*");
Matcher m = p0.matcher(data);
while (m.find())
{
String fullMatch = m.group();
int n = 0;
for (String s : fullMatch.split("\r\n|[\r\n]"))
{
System.out.printf("line %d: %s%n", n++, s);
}
}
output:
line 0: truck
line 1: zDoug
line 2: Doug's house
line 3: (123) 456-7890
line 4: Edoug@doug.com
line 5: 30
line 6: 61234.56
line 7: 8/10/2003
I'm also assuming each line of data contains at least one character, and that the blank lines between data block are really empty--i.e., no spaces, TABs, or other invisible characters.
(BTW: To test that regex in RegExr, remove the (?m) and check the multiline box instead. RegExr is powered by ActionScript, so the rules are a little different. For a Java-powered regex tester, check out RegexPlanet.)
answered 2011-03-03T07:21:40.690