I need a regular expression that will find all the numbers on a sentence. For example: \"I have 3 bananas and 37 balloons\" I will get:
3
37
\"The time i
The regex you are looking for is [0-9]+
or \d+
. You should then get multiple matches for the sentence.
The regex itself is as simple as \d+
, but you will also need to set a flag to match it globally, the syntax of which depends on the programming language or software you are using.
EDIT: Some examples:
Python:
import re
re.findall(r"\d+", my_string)
JavaScript:
myString.match(/\d+/g)
Split your string by [^0-9]+
.
JAVA: String[] numbers = "yourString".split("[^0-9]+");
JavaScript: var numbers = "yourString".split(/[^0-9]+/);
PHP: $numbers = preg_split("/[^0-9]+/", "yourString");