-2

I have the following strings where I would like to identify numbers, that can be sometimes followed by any character. For example:

  • Cats 10, Dogs 3?
  • Cats 10 Dogs 3
  • Cats 10. Dogs 3@

I should be able to find 10 and 3 in each of these strings.

I tried s.matches("[0-9,.@!]+")) but in this case I don't catch numbers not followed by special characters.

Kiwi
  • 173
  • 2
  • 9
  • Maybe all you need is [How to extract numbers from a string and get an array of ints](https://stackoverflow.com/questions/2367381/how-to-extract-numbers-from-a-string-and-get-an-array-of-ints?) – Wiktor Stribiżew Sep 02 '20 at 20:43

2 Answers2

-1

Can you use the below?

s.matches("\d+")
Mohamad.K
  • 34
  • 5
-1

maybe try

s.matches("[0-9].?");

. is any character

? is zero or more

Ela Łabaj
  • 74
  • 6