-1

I have a string and I want to a) check if it matches the following format and b) extract the numbers and text into variables:

"x:x:x - some text" // x = some integer number

In, C, I would use :

sscanf(str1, "%d:%d:%d - %s\n", &x, &y,&z, str2);

How do I do the same in Java?

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
minime
  • 155
  • 1
  • 8

2 Answers2

1

Did you mean :

String text = "1:99:33 - some text";
boolean check = text.matches("\\d+:\\d+:\\d+ - .*");
System.out.println(check);

If you want to match exact (one number):(two numbers):(two number) you can use \\d:\\d{2}:\\d{2} instead of \\d+:\\d+:\\d+

details

  • \\d+ match one or more digit
  • : literal character
  • \\d+ match one or more digit
  • : literal character
  • \\d+ match one or more digit
  • - one space hyphen one space
  • .* zero or more any character

...how do I extract the numbers and the text from the string?

If you are using Java 8 you can split your input, the first input return numbers separated by :, the second is the text you want, so to extract the numbers, you need to split the first input again by : then Iterate over them and convert each one to an Integer, like this :

String input = "1:99:33 - some text";
String[] split = input.split(" - ");//Split using space hyphen space
String text = split[1];//this will return "some text"
List<Integer> numbers = Arrays.asList(split[0].split(":")).stream()
        .map(stringNumber -> Integer.parseInt(stringNumber))
        .collect(Collectors.toList());// this will return list of number [1, 99, 33]
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • Thanks. That works. But how do I extract the numbers and the text from the string? Do I pass the same format string to a Pattern object? – minime Dec 24 '17 at 07:24
  • @minime what did you mean `extract the numbers and the text from the string` can show us an input output example? – Youcef LAIDANI Dec 24 '17 at 07:25
  • In the original question, the numbers and text and read into variables. How can I do that in Java? – minime Dec 24 '17 at 07:26
  • @minime In the example i share `"1:99:33 - some text"` did you want `1:99:33` and `some text` separately ? – Youcef LAIDANI Dec 24 '17 at 07:28
  • I should have mentioned that I want a) the string to match a specific format and b) to extract the numbers and text. – minime Dec 24 '17 at 07:29
  • @minime please [edit](https://stackoverflow.com/posts/47958786/edit) your question and make an example about what you want exactly – Youcef LAIDANI Dec 24 '17 at 07:30
  • check my edit @minime hope this can help you – Youcef LAIDANI Dec 24 '17 at 07:42
  • 1
    The code works perfectly. I need to read more data in various formats and I will use this code as a template. Thank you very much. – minime Dec 24 '17 at 07:49
0

The alternatve solution:

String input = "1:99:33 - some text";
    StringTokenizer st = new StringTokenizer(input, ":");

    while (st.hasMoreTokens()) {
        String token = st.nextToken();
        System.out.println(token);
    }

StringTokenizer break the string into tokens. Where ":" is a tokens delimiter. Output:

1
99
33 - some text
slawek
  • 328
  • 1
  • 10