2

I am trying to get only the number part out of a string that results after a search query that looks something like this (142 items found, displaying 1 To 30.) I want to save only "142" for later use.

This is my code so far.

private UserSearchPage selectGutsPartner() {
    find(PARTNER).sendKeys(PARTNER_NAME);
    find(SEARCH_BUTTON).click();
    return new UserSearchPage(driver);
}

public String getItemsOnPage() {
    selectGutsPartner();
    return find(ITEMS_FOUND).getText();
}

public String getOnlyNumberOfItems() {
    Pattern pat = Pattern.compile("(//d+) items found, displaying *.");
    String text = getItemsOnPage();
    Matcher matcher = pat.matcher(text);
    System.out.println(matcher.group(1));
    return text;
}

But the number is not striped out.

Thanks.

Arran
  • 24,648
  • 6
  • 68
  • 78
edward.eas
  • 59
  • 1
  • 3
  • 9
  • Why not `1` and `30` from `displaying 1 to 30` – sshashank124 Apr 10 '14 at 12:06
  • Split by `" "` and take the first item in the resulting array? – ccheneson Apr 10 '14 at 12:07
  • 2
    What is going on with `//`. Maybe you mean `\\\` in your pattern. – Floris Apr 10 '14 at 12:09
  • 1
    The ` *.` at the end means _"0 or more space characters followed by any character"_, which is not what you want. I think you wanted to use `.*` which means _"any character, repeated 0 or more times"_. Which is useful, because you don't capture it, and you don't use `$`. – Benoit Duffez Apr 10 '14 at 12:28

5 Answers5

3

use

String var="142 items found, displaying 1 To 30.";
return Integer.parseInt(var.split(" ")[0]);
Deepu--Java
  • 3,742
  • 3
  • 19
  • 30
1

To get all numbers:

int i = Integer.parseInt("blah123yeah4yeah".replaceAll("\\D", ""));
// i == 1234

And for just the first number:

int i = Integer.parseInt("x-42x100x".replaceAll("^\\D*?(-?\\d+).*$", "$1"));
// i == -42

Already answered here: number out of string in java

Community
  • 1
  • 1
Mardoz
  • 1,617
  • 1
  • 13
  • 26
1

Try

 Pattern pat = Pattern.compile("(\\d+) items found, displaying.*");

backslash, not forward slash. And period in front of the * (since the * is a quantifier for the "thing that precedes it", and you want that "thing" to be "any character", not "any number of spaces". But you don't really need it at all. If you stopped at items found it would work just fine (and still work if everything fitted on one page, which might change the message):

 Pattern pat = Pattern.compile("(\\d+) items found");
Floris
  • 45,857
  • 6
  • 70
  • 122
1

Using java regex here is the a working method

public String getOnlyNumberOfItems(String text) {
    Pattern pattern = Pattern.compile("^[0-9]+  items found, displaying*");
    Matcher textVerifyMatcher = pattern.matcher(text);

    if(textVerifyMatcher.find()){
        System.err.println("The text is not in the correct format");
        return null;
    }

    Pattern numPattern = Pattern.compile("^[0-9]+ ");
    Matcher m = numPattern.matcher(text);

    if(m.find()){
        text = m.group(0);
        System.out.println("Number: " + m.group(0));
    }
    return text;
}

if the given text is not in the right format the method will return null, it would be better to return an exception though.

Sahar Rabinoviz
  • 1,939
  • 2
  • 17
  • 28
0

import com.google.common.base.CharMatcher; import com.google.common.base.Splitter;

public class Test {

public static void main(String[] args) {

String str = "142 items found, displaying 1 To 30";
Iterable<String> res = Splitter.on(CharMatcher.JAVA_LETTER).split(str);

}

}

Ran Adler
  • 3,587
  • 30
  • 27