I have a text file that looks like this:
IX: {Series|(} {2}
IX: {Series!geometric|(} {4}
IX: {Euler's constant} {4}
IX: {Series!geometric|)} {4}
IX: {Series!arithmetic|(} {4}
IX: {Series!arithmetic|)} {5}
IX: {Series!harmonic|(} {5}
IX: {Euler's constant} {5}
IX: {Series!harmonic|)} {5}
IX: {Series|)} {5}
What I would like to do is just get the Strings within the "{" and "}" for each string and for each occurrence. Meaning that for the first string for example, I could get "Series|(" and "2". I am having a lot of trouble coming up with how to approach this. I know I can split by "\{" but that would still leave the ending braces included, meaning I would have to go through each element in the split array and further subdivide it, which isn't clean and doesn't seem to work well for various cases. Is there a different method to approach this?
I've tried using substring so far:
File file = new File("test.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String line = null;
while((line = br.readLine()) != null){
String[] parts = line.split("\\{");
String p = parts[1].substring(0,parts[1].length()-1);
System.out.println(p);
System.out.println("");
}
but this returns - using the top string as an example - "Series|(}", when what I want is "Series|(". The problem with this method is also that the strings seem to be differing in length so whatever number I put to substract from the string length, it is not applicable to all the strings.
I understand that regular expressions would be more useful here, but they are quite confusing for me and I am having trouble understanding them in the time I have left. If there is any other method that is simpler I would really appreciate it.
EDIT: I have tried the following:
String line = "IX: {Series|(} {2}";
Pattern pattern = Pattern.compile("\\{(.*?)\\}");
Matcher matcher = pattern.matcher(line);
System.out.println(matcher.group(1));
But I receive a no match found error. Can someone explain where my regular expression is incorrect? I was going off of the question posted here: How to extract a substring using regex