Link to home
Start Free TrialLog in
Avatar of E R
E R

asked on

Java matcher and pattern code.

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.

User generated image
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);
            }
        }

    }
Avatar of _agx_
_agx_
Flag of United States of America image

Could you please post the code as text?
Avatar of E R
E R

ASKER

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.
Avatar of E R

ASKER

I paste it in comments. Sorry.
Avatar of E R

ASKER

I have pasted in orignal question.
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?
Avatar of E R

ASKER

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.
Avatar of E R

ASKER

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));
                }
            }
        }
Avatar of E R

ASKER

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.
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?
Avatar of E R

ASKER

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

You don't need to do all that space searching btw. Put it in the pattern
ASKER CERTIFIED SOLUTION
Avatar of Jan Louwerens
Jan Louwerens
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of E R

ASKER

Thanks all