|
|
0鱼币
本帖最后由 什么鬼… 于 2017-7-15 18:22 编辑
一开始我是这么写的
- >>> re.findall(r"\d+",'123')
- ['123']
复制代码
但是面临问题就是
- >>> re.findall(r'\d+','123.321')
- ['123', '321']
复制代码
这样不行(楼主强迫症)
于是尝试修改
- >>> re.findall(r'\A\d+\Z','1231244.323123')
- []
复制代码
但是又面临问题
- >>> re.findall(r'\A\d+\Z','the int is 123,and float is 1.3213')
- []
复制代码
无法匹配出中间的数字
考虑数字前面和后面都不能有小数点,所以再尝试
- >>> re.findall(r"(?<!\.)\d+(?!\.)",'the int is 123,and float is 789.987')
- ['123', '78', '87']
复制代码
然后问题又来了,因为前面不能是小数点,所以python直接丢掉了一个数字和一个小数点,所以楼主又进行更改
- >>> re.findall(r'(?<!\.\d*)\d+(?!\d*\.)','the int is 123,and float is 789.987')
- Traceback (most recent call last):
- File "<pyshell#57>", line 1, in <module>
- re.findall(r'(?<!\.\d*)\d+(?!\d*\.)','the int is 123,and float is 789.987')
- File "D:\python3\lib\re.py", line 222, in findall
- return _compile(pattern, flags).findall(string)
- File "D:\python3\lib\re.py", line 301, in _compile
- p = sre_compile.compile(pattern, flags)
- File "D:\python3\lib\sre_compile.py", line 566, in compile
- code = _code(p, flags)
- File "D:\python3\lib\sre_compile.py", line 551, in _code
- _compile(code, p.data, flags)
- File "D:\python3\lib\sre_compile.py", line 160, in _compile
- raise error("look-behind requires fixed-width pattern")
- sre_constants.error: look-behind requires fixed-width pattern
复制代码
这次比较惨,因为反向否定界定必须定宽,所以直接报错了。。。。
到这里楼主就没办法了.....估计哪个地方没想到....来求助一波,希望小伙伴们开开脑洞解决一波......
后期声明....the int is 123,and float is 789.987只是楼主的一个例子,想要匹配到前面的123,而不能匹配到后面的所有数据
- >>> re.findall(r'(?<![.\d])\d+(?![.\d])',ss)
- ['123']
复制代码
|
|