Python Regex从字符串中提取最大数值
使用正则表达式从字符串中提取最大数值的最简单方法是-
使用regex模块从字符串中提取所有数字
从这些数字中找到最大值
例如,对于输入字符串-
这个城市有121005人,邻近城市有1587469人,而遥远城市有18775994人。
我们应该得到输出-
18775994
我们可以使用“\d+”正则表达式来查找字符串中的所有数字,因为\d表示一个数字,而加号则找到最长的连续数字字符串。我们可以使用re包实现它,如下所示:
import re
# Extract all numeric values from the string.
occ = re.findall("\d+", "There are 121005 people in this city, 1587469 in the neighbouring city and 18775994 in a far off city.")
# Convert the numeric values from string to int.
num_list = map(int, occ)
# Find and print the max
print(max(num_list))这将给出输出-
18775994
热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿