regex - 使用正则表达式从字符串中获取不同格式的数字
问题描述
我需要从字符串中获取这些数字格式
20000
20 000
20 000 000
20 000,00
20 000.00
2000,000
200000.000
字符串可以
some text 20000
20000 some text
到目前为止我有这个
/((\d){1,}[ .,]{1}[0-9]{1,}[ .,]{1}[0-9]{1,})/g
谢谢你。
解决方案
我会使用这个正则表达式:
^[1-9][0-9]{0,2}(?:[ ,]?\d{3})*(?:[,.]\d+)?$
演示
这是正则表达式的解释:
^ from the start of the string
[1-9][0-9]{0,2} match 1 to 3 leading digits (first digit not zero)
(?:[ ,]?\d{3})* then match an optional thousands separator followed by 3 digits,
the entire quantity zero or more times
(?:[,.]\d+)? match an optional decimal component, using either , or . as the separator
$ end of the string
推荐阅读
- vba - 如何让它在当前查看文件夹上工作
- django - 在 django admin 中自动注册某些模型的问题
- python - 如何使用 Numpy 有效地创建条件列数组?
- javascript - 如何在nodejs中形成一个动态的json数组?
- postgresql - 是否可以保证 postgres 中并行聚合函数的最小工作人员数量?
- java - 如何将 YML 值放入 @Pattern(regexp = "HELLO|WORLD")
- vb.net - 如何使用 PostgreSQL 作为数据库和 vb.net windows 窗体在 DataGridView1 中发布数据
- python - 如何为具有扩展用户(onetoone 字段)关系的模型执行 put/patch 方法 django rest框架
- jquery - 从 ajax 调用传回的数据中选择特定项目
- sql - 简化 Oracle 中的现有查询