Skip to content

Commit

Permalink
re
Browse files Browse the repository at this point in the history
  • Loading branch information
jackzhenguo committed Mar 21, 2021
1 parent 360e122 commit 0423c87
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@
| 194 | [python对象转json对象](md/194.md) | python json | v1.0 | ⭐️⭐⭐⭐ |
| 195 | [发现列表前3个最大或最小数](md/195.md) | list heapq | v1.0 | ⭐️⭐⭐⭐ |
| 196 | [使用堆排序列表为升序](md/196.md) | sort heapq | v1.0 | ⭐️⭐⭐⭐ |
| 197 | [使用正则提取正整数和大于0的浮点数](md/197.md) | re findall | v2 | ⭐️⭐⭐⭐ |




### Python 实战


Expand Down
37 changes: 36 additions & 1 deletion md/197.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,41 @@
@version
@date 2020/04/04
```

下面正则适用于提取正整数和大于0的浮点数,参看我的,若有疏漏欢迎补充。

```python
>>> import re
>>> pat_integ = '[1-9]+\d*'
>>> pat_float0 = '0\.\d+[1-9]'
>>> pat_float1 = '[1-9]\d*\.d+'
>>> pat = 'r%s|%s|%s'%(pat_float_0,pat_float_1,pat_integ)
>>> re.findall(pat, r)
['0.78', '3446.73', '0.91', '13642.95', '1.06', '2672.12', '3000']
```

排除这些串:

000
000100
0.00
000.00


解释:`*`表示前一个字符出现0次或多次,`+`表示前一个字符出现1次或多次,`\d`表示数字[0-9]`[1-9]`表示1,2,3,4,5,6,7,8,9,`\.`表示小数点

主要考虑:正整数最左侧一位大于0,大于1的浮点数必须以[1-9]开始,大于0小于1的浮点数小数点前只有1个0.




Day163:使用Python正则 提取出输入一段文字中的所有浮点数和整数 #Python拆书1#

例如: 截至收盘,上证指数涨0.78%,报3446.73点,深证成指涨0.91%,报13642.95点,创业板指涨1.06%,报2672.12点。指数午后震荡走高,碳中和概念强者恒强,板块内上演涨停潮,环保、物业、特高压板块午后涨幅扩大,数字货币板块尾盘冲高,钢铁、煤炭、有色板块全天较为低迷,题材股午后整体回暖,两市上涨个股逾3000家,赚钱效益较好。

提取出所有浮点数和整数: 0.78, 3446.73, 0.91,13642.95 等



<center>[上一个例子](196.md) [下一个例子](198.md)</center>
<center>[上一个例子](196.md) [下一个例子](198.md)</center>

0 comments on commit 0423c87

Please sign in to comment.