anonymous
anonymous

Reputation: 228

How do I convert to object different pattern in java

I have a set of data about book.
I have to convert the data to object. Line(book, chapter, verse, content)

The structure is
Book Chapter:Verse[tab]Content[Enter]

Example
deuteronomy 7:7 the lord did not set his love upon you, nor choose you, because ye were more in number than any people; for ye were the fewest of all people:

1 samuel 17:6 and he had greaves of brass upon his legs, and a target of brass between his shoulders.

As the book structure is different.
1 samuel and deuteronomy
1 samuel consist a space. how do i convert these data to object in java

I have already divide into two part
StringTokenizer itr = new StringTokenizer(value.toString().toLowerCase(),"\t");
I think I should use regex to split them. But I don't know how to do that.

Upvotes: 0

Views: 85

Answers (1)

Thomas
Thomas

Reputation: 88707

Have a look at regular expressions. If the content doesn't contain any new line characters (i.e. \n which I assume is what you mean by [Enter]) you can try the following expression:

(.*?) (\d+):(\d+)\t([^\n]+)

Break down of the expression:

  • (.*?) matches anything but as little as possible and collects it into group 1. The match ends with a space which is not selected - this is the book
  • (\d+) matches any sequence with at least 1 digit and captures it into a group. So (\d+):(\d+) captures chapter and verse into groups 2 and 3.
  • \t([^\n]+) matches a tab followed by a sequence of at least 1 non-new line character which is captured into group 4. This is the content.

For working with regex like this, have a look at the classes Pattern and Matcher.

Upvotes: 1

Related Questions