Java matcher and pattern code.

E R
E R used Ask the Experts™
on
I am working with matcher and pattern in Java. I think this test code should work to add and print my desired search but it is not. If anyone can take a look and fix this it would be much appreciated. Note the if I just use the while loop it works fine. It is when I add in the for loop to iterate through the array list is when it prints out blank.

Screen-Shot-2018-08-29-at-11.51.28-A.png
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class test {


    public static void main(String[] args) {

        ArrayList<String> linet = new ArrayList<>();
        ArrayList<String> lines = new ArrayList<>();

        lines.add("O#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO O#769117789:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]");
        lines.add("SO#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO O#769165189:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]");
        lines.add("O#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO SO#7696561189:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]");
        lines.add("CASE-1443453931800\tPN#109684324201-2 qty1EA\t\"");

        for (int i = 0; i < lines.size(); i++) {

            String line = lines.get(i);

            String so1 = "SO#";
            Pattern p = Pattern.compile(so1);
            Matcher m = p.matcher(line);

            while (m.find() == true) {


                int j = m.start();
                boolean foundspace = false;

                while (foundspace == false) {

                    if (line.charAt(i) == ' ') {
                        foundspace = true;
                    }
                    j++;

                }

                String d = line.substring(m.start(), j);
                linet.add(d);

            }

            System.out.println(linet);
            }
        }

    }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2015

Commented:
Could you please post the code as text?
E RData Analyst

Author

Commented:
public class test {


    public static void main(String[] args) {

        ArrayList<String> linet = new ArrayList<>();
        ArrayList<String> lines = new ArrayList<>();

        lines.add("O#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO O#769117789:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]");
        lines.add("SO#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO O#769165189:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]");
        lines.add("O#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO SO#7696561189:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]");
        lines.add("CASE-1443453931800\tPN#109684324201-2 qty1EA\t\"");

        for (int i = 0; i < lines.size(); i++) {

            String line = lines.get(i);

            String so1 = "SO#";
            Pattern p = Pattern.compile(so1);
            Matcher m = p.matcher(line);

            while (m.find() == true) {


                int j = m.start();
                boolean foundspace = false;

                while (foundspace == false) {

                    if (line.charAt(i) == ' ') {
                        foundspace = true;
                    }
                    j++;

                }

                String d = line.substring(m.start(), j);
                linet.add(d);

            }

            System.out.println(linet);
            }
        }

    }
It is MUCH better to paste your code into the question than to include a screenshot.

Maybe I have a simple fix I would like to try, I if I have to retype the whole thing I will be moving on.
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

E RData Analyst

Author

Commented:
I paste it in comments. Sorry.
E RData Analyst

Author

Commented:
I have pasted in orignal question.
Top Expert 2016

Commented:
It is MUCH better to paste your code into the question than to include a screenshot.
And better still to post it in code tags

What are you trying to do exactly?
E RData Analyst

Author

Commented:
My overall goal is to extract SO#833452-14943408 from the string in the array list from my test string. This is part of a larger program in which I am reading in lines from a file line by line and trying to extract any instance of "SO#" and the subsequent following number and add it to an array list.
E RData Analyst

Author

Commented:
I have modified the code a bit and I think I am heading in the right direction but now it prints out iterations of the array. Code below.



import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class test2 {


    public static void main(String[] args) {

        ArrayList<String> linet = new ArrayList<>();
        ArrayList<String> lines = new ArrayList<>();
        String so1 = "SO#";

        lines.add("O#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO O#769117789:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]");
        lines.add("SO#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO O#769165189:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]");
        lines.add("O#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO SO#7696561189:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]");
        lines.add("CASE-1443453931800\tPN#109684324201-2 qty1EA\t\"");


        for (int i = 0; i < lines.size(); i++) {

            String line = lines.get(i);

            Pattern p = Pattern.compile(so1);
            Matcher m = p.matcher(line);

            while (m.find() == true) {

                int j = m.start();
                boolean foundspace = false;

                while (foundspace == false) {

                    if (line.charAt(j) == ' ') {
                        foundspace = true;
                    }
                    j++;
                }

                String d = line.substring(m.start(), j);
                linet.add(d);

            }


        }

                for (int k = 0; k < linet.size(); k++) {

                    System.out.println(linet.get(k));
                }
            }
        }
E RData Analyst

Author

Commented:
So I guess my question becomes a bit easier. Given the updated comment code. I get my end result but I get every iteration of my result and I just want the last one. My problem is how to pass a variable from inside a loop to outside of it or is there another way to write this code. I am new to java.
Top Expert 2016

Commented:
I get my end result but I get every iteration of my result and I just want the last one.
Don't quite follow that. I thought you wanted all SOs? Do you mean you want all unique instances of that or just one? Confused

Also, you're reading finally lines from a file are you?
E RData Analyst

Author

Commented:
I actually just figured this out. Below is the code that prints the desired  output. Thanks

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class test2 {


    public static void main(String[] args) {

        ArrayList<String> linet = new ArrayList<>();
        ArrayList<String> lines = new ArrayList<>();
        String so1 = "SO#";

        lines.add("SO#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO O#769117789:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]");
        lines.add("S)#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO O#769165189:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]");
        lines.add("O#833452-14943408 PO#Subject: Re: gg  - SO#Oops. Your O#purchase order is missing key payment information.PPO SO#7696561189:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]");
        lines.add("CASE-1443453931800\tPN#109684324201-2 qty1EA\t\"");


        for (int i = 0; i < lines.size(); i++) {

            String line = lines.get(i);

            Pattern p = Pattern.compile(so1);
            Matcher m = p.matcher(line);

            while (m.find() == true) {

                int j = m.start();
                boolean foundspace = false;

                while (foundspace == false) {

                    if (line.charAt(j) == ' ') {
                        foundspace = true;
                    }

                    j++;
                }

                String d = line.substring(m.start(), j);
                linet.add(d);
            }



        }

                for (int k = 0; k < linet.size(); k++) {

                    System.out.println(linet.get(k));
                }
            }
        }
I see this is your first question on ExEx.  If you are going to be posting more code samples, you might want to use the CODE tags to display your program snippets.  You can cut and paste your code into the question, then select it, and click the CODE icon on the menu bar.  Ckick on Preview to see the result.
        ArrayList<String> linet = new ArrayList<>();
        ArrayList<String> lines = new ArrayList<>();
        String so1 = "SO#";

        lines.add("SO#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO O#769117789:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]");
        lines.add("S)#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO O#769165189:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]");

Open in new window

Top Expert 2016

Commented:
You don't need to do all that space searching btw. Put it in the pattern
Software Engineer
Commented:
      List<String> lines = Arrays.asList(
         "SO#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO O#769117789:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]",
         "S)#833452-14943408 PO#Subject: Re: gg  - Oops. Your O#purchase order is missing key payment information.PPO O#769165189:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]",
         "O#833452-14943408 PO#Subject: Re: gg  - SO#Oops. Your O#purchase order is missing key payment information.PPO SO#7696561189:C11134 [ ref:_00D30dWxY._5001311TVgB:ref ]",
         "CASE-1443453931800\tPN#109684324201-2 qty1EA\t\"");
      
      List<String> results = new ArrayList<>();
      
      Pattern pattern = Pattern.compile("SO#\\S+");
      
      for (String line : lines)
      {
         Matcher matcher = pattern.matcher(line);
         while (matcher.find())
            results.add(matcher.group());
      }
      
      for (String result : results)
         System.out.println(result);

Open in new window

E RData Analyst

Author

Commented:
Thanks all

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial