Hi, I need help creating or manipulationg my current regex that will grab two numbers from a line, while excluding a specific number. Here is an example of what the line will look like:

"In our tests, downloads on port 6881 achieved up to 12059 Kbps while downloads on port 47649 achieved up to 9623 Kbps."

My regular expression is this: Pattern pattern = Pattern.compile("^In our tests, downloads on port 6881\D+(\d+)\D+(\d+)"); . So I need help with a regex that will grab the 12059 number and the 9623 number, and exclude 47649. In my program the numbers 6881 and 47649 will always be the same. The 12059 and 9623 numbers will change depending on the file being read.

Any and all help is extremly appreciated. If you need me to give any more information or explanation let me know.

"^In our tests, downloads on port 6881\D+(\d+)\D+(\d+)"

If that string works for you, you could easily do...

"^In our tests, downloads on port 6881\D+\d+\D+(\d+)\D+\d+\D+(\d+)"

Anything inside the parentheses are the group you are caturing from the match.

I'm sorry I should said "Pattern pattern = Pattern.compile("^In our tests, downloads on port 6881\D+(\d+)\D+(\d+)");" is NOT working for me. But I think it is close. I just tried "^In our tests, downloads on port 6881\D+\d+\D+(\d+)\D+\d+\D+(\d+)" and that did not work either.

Oh I didn't carefully look at your previous post. It should be.

"^In our tests\D+\d+\D+(\d+)\D+\d+\D+(\d+)"

I'm sorry I should said "Pattern pattern = Pattern.compile("^In our tests, downloads on port 6881\D+(\d+)\D+(\d+)");" is NOT working for me. But I think it is close. I just tried "^In our tests, downloads on port 6881\D+\d+\D+(\d+)\D+\d+\D+(\d+)" and that did not work either.

It really need not be that complicated. If you are sure of the format of the text, the simplest regex would be as shown in the below code:

public static void main(final String[] args) {
    final String sample = "In our tests, downloads on port 6881 achieved up to 12059 Kbps " +
            "while downloads on port 47649 achieved up to 9623 Kbps.";

    final Pattern pat = Pattern.compile(".+?(\\d+) Kbps.+?(\\d+) Kbps.*");
    final Matcher matcher = pat.matcher(sample);
    if(matcher.matches()) {
        int speed1 = Integer.parseInt(matcher.group(1));
        int speed2 = Integer.parseInt(matcher.group(2));
        System.out.printf("Speed 1 => %s and Speed 2 => %s", speed1, speed2);
    } else {
        System.out.println("No match found");
    }
}

This assumes that the speed is always followed by a space and then Kbps but I guess that's a fair enough assumption.

I suggested the same pattern that someone else suggested and the OP accepted. I didn't want to give him any new pattern. Anyway, your pattern is much better but the OP won't understand anything because he/she doesn't understand even with very few symbols... :(

That regex works Taywin, you are a life saver. I really appreciate your help. You are right in that I don't really understand the regular expressions, but I am looking at references online to try to understand exactly how your regex works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.