0

In a text file I have Chapters and Verses and I need to extract the chapter numbers and verse numbers.

The format for the chapters is ["CHAPTER "] [number]

The format for the verses is [number] [text]

I have a regular expression which now prints out the chapter number in document once a word is searched as if the word is contained within that chapter however, for the verse part it prints out the entire verse and now just the number. I need the number of the verse not the text. This is the code so far with the regex I'm using which returns the chapter number and text contained within the verse.

  for(String a : list)
          {

              if(a.contains(word)){

                  String patt = "((?<chapter>CHAPTER\\s\\d{1,3}) (?<verse>\\d{1,3})(?<verseText>.*))|(^(?<verse2>\\d{1,3})(?<verseText2>.*))";
                  //String patt =  "^?(\\w*+)(\\d*+)(\\d*+)";
                  String book = "";
                  String chapter = " ";
                  String verse = " ";

                  Pattern pattern = Pattern.compile(patt);
                  Matcher match = pattern. matcher(a);
                    if(match.find()){
                         chapter = match.group();

                    }

                        System.out.println("[" +book +" " +chapter +" : " +verse +"]");     
                }   

How would I extend this to have it search for the number of the verse instead of the text, the verses are listed per line in a text document and the number of the verse is at the beginning of each line. Thanks for the help.

This is the format of the text documents:

NAME OF THE BOOK

CHAPTER 1 1 This is a verse 2 This another verse 3 This is verse 3 4 This is verse 4

CHAPTER 2 1 This is a verse for chapter 2 2 This another verse for chapter 2 3 This is verse 3 for chapter 2 4 This is verse 4 for chapter 2

This is continuous for multiple chapters for multiple verses and text files. Instead of the text of the verse, I need the number of the verse returned. The format it needs to be output is [bookName chapter_number : verse_number]

Could someone help me with another regex which gets the book name too, thanks.

Hydra
  • 3
  • 2
  • can you please give an example of the input chapter line and verse line? – MihaiC Dec 11 '14 at 20:09
  • You say you have a regular expression that prints something out, but regular expressions don't print anything. It's the rest of your code that prints stuff. And we probably need to see it. – ajb Dec 11 '14 at 20:19
  • ok I added everything I could – Hydra Dec 11 '14 at 20:44
  • looks like duplicate to http://stackoverflow.com/questions/27406173/using-regular-expressions-to-extract-values-in-java?rq=1. Can you refer this. – Priyanka Kotari Dec 11 '14 at 20:48
  • What is contained in *one* element of `list` - a single line, a whole chapter, or a whole book? – Armali Mar 21 '17 at 09:52

0 Answers0