Find numbers in a sentence by regex

后端 未结 3 818
自闭症患者
自闭症患者 2021-01-25 20:43

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

3条回答
  •  一向
    一向 (楼主)
    2021-01-25 21:08

    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");

提交回复
热议问题