通过正则表达式查找句子中的数字 [英] Find numbers in a sentence by regex

查看:60
本文介绍了通过正则表达式查找句子中的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个正则表达式来查找句子中的所有数字.例如:我有 3 个香蕉和 37 个气球"我会得到:

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

时间是20:00,我有7辆坦克"我会得到:

"The time is 20:00 and I have 7 tanks" I will get:

20

00

7

推荐答案

正则表达式本身和 \d+ 一样简单,但是你还需要设置一个标志来全局匹配它,语法其中取决于您使用的编程语言或软件.

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.

一些例子:

蟒蛇:

import re
re.findall(r"\d+", my_string)

JavaScript:

JavaScript:

myString.match(/\d+/g)

这篇关于通过正则表达式查找句子中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆