What is the regex for:
somewhere in the string find "wordA" followed by anything till "wordB"?
I'm able to get the tags but due to the nested tags it adds too many tags. I think the solution would be in a proper match of the opening and closing tag. I can't find a website with properly described regex rules that is not either overwhelming or very obtuse. Please help me find a good one. Would really appreciate it!!
package com.codegym.task.task19.task1918;
/*
Introducing tags
/Users/lilianetop/Desktop/taak1917.txt
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Solution {
public static void main(String[] args) throws IOException {
//1. The program must read the file name from the console (use BufferedReader).
//2. try-with resources closes the stream automatically
String fileName;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
fileName = reader.readLine();
}
//3. The program must read the file's contents (use FileReader).
//4. The file input stream (FileReader) must be closed. (autoclosable)
//5. The program must write to the console all the tags that match the tag specified by the argument passed to the main method.
try (BufferedReader reader2 = new BufferedReader(new FileReader(fileName))) {
String line;
//problem is that the closing tag is on the next line so it is perceived as another String so I have to use a Stringbuilder
StringBuilder stringBuilder = new StringBuilder();
while (reader2.ready()) {
line = reader2.readLine();
stringBuilder.append(line);
}
//if args[0] = "span"; openingTag = <span and closing </span
String openingTag = "<" + args[0];
String closingTag = "</" + args[0] + ">";
String result = stringBuilder.toString();
Pattern pattern1 = Pattern.compile(openingTag);
Pattern pattern2 = Pattern.compile(closingTag);
Matcher matcher1 = pattern1.matcher(result);
Matcher matcher2 = pattern2.matcher(result);
while(matcher1.find() && matcher2.find()){
System.out.println(result.substring(matcher1.start(), matcher2.end()));
}
/*if (result.contains(openingTag)) {
//it only finds 1 pair how to get them all?
System.out.println(result.substring(result.indexOf(openingTag), result.indexOf(closingTag) + closingTag.length()));
}*/
}
}
}