-1

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

Community
  • 1
  • 1
Jeremy Fisher
  • 2,510
  • 7
  • 30
  • 59

2 Answers2

1

You should split() the string a second time, by the closed braces.

File file = new File("test.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String line = null;
while((line = br.readLine()) != null) {
    if (line.contains("{")) {
        String[] parts = line.split("\\{");
        String p = parts[1].substring(0,parts[1].length()-1);
        parts = p.split("\\}");
        System.out.println(parts[0] + "\n"); 
    }
}
La-comadreja
  • 5,627
  • 11
  • 36
  • 64
  • I would like to avoid this since I also have to split the other string that contains {2} in order to get "2" as well. – Jeremy Fisher Oct 18 '14 at 23:57
  • What do you mean by "split the other string that contains {2} in order to get '2'"? What is the input and the output of that? – La-comadreja Oct 18 '14 at 23:58
  • Taking the first line in my text file for example, I want to obtain the strings "Series|(" as well as "2" since they are both within curly braces. I just need whatever is in curly braces. I would like to avoid the splitting method if possible. – Jeremy Fisher Oct 19 '14 at 00:00
  • I know regular expressions would work better for this, I just have no clue how to use them and I don't have the luxury of time to learn the correct patterns to do what I'm looking for. – Jeremy Fisher Oct 19 '14 at 00:00
  • split() is fine and turning the string into an array doesn't change the string. You can call split() again on the old string or a subsequent element of the array, and get "2". – La-comadreja Oct 19 '14 at 00:01
0

Thanks to la-comahedra for his solution which works well. In case anyone wanted to do it with regular expressions however, here is the solution I found:

String line = "IX: {Series|(}               {2}";
        Pattern pattern = Pattern.compile("\\{(.*?)\\}");
        Matcher matcher = pattern.matcher(line);
        while(matcher.find()){
            System.out.println(matcher.group(1));
        }

This solution is based off of the solution in the question: How to extract a substring using regex.

The output of the above code would be:

Series|(
2
Community
  • 1
  • 1
Jeremy Fisher
  • 2,510
  • 7
  • 30
  • 59