I have a text file which contains lines, and some of them are in the following format:
- 3 tabs,
- after if few words and line break at the end.
- I need to catch the words in these lines, one by one (with the index of each word in the text).
I thought about a solution using 2 regex patterns and 2 loops (added the code below), but I would like to know if there is a better solution using only one regex pattern.
Here is an example for lines from the text:
Hello I am studying regex!
This is a line in the text.
Don't need to add this line
nor this line.
But this line should be included.
Map<String, Integer> wordsMap = New HashMap<>();
Pattern p = Pattern.compile("\\t{3}(.*)\\n");
Matcher m = p.matcher(text);
Pattern p2 = Pattern.compile("(\S+)");
Matcher m2 = p.matcher(");
while(m.find()) {
m2.reset(m.group(1));
while(m2.find()) {
wordsMap.add(m2.group(1), m.start(1) + m2.start(1));
}
}