python 怎么取出字符串中的数字子串? Python怎么用正则表达式提取字符串中的数字和子字符串

python\u4e2d\u5982\u4f55\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u6570\u5b57\uff1f

1\u3001\u5982\u4e0b\u56fe\uff0c\u8981\u63d0\u53d6#\u540e\u9762\u7684\u5b57\u7b26\uff0c\u4e5f\u5373\u7ea2\u8272\u7684\u201cSDK\u201d\u5230B\u5217\u3002


2\u3001\u9996\u5148\uff0c\u5728B2\u4e2d\u8f93\u5165\u516c\u5f0f\uff1a
=FIND("#",A2)
\u8fd4\u56de#\u5728\u5b57\u7b26\u4e32\u4e2d\u7684\u4f4d\u7f6e\uff0c#\u5728A2\u5355\u5143\u683c\u6587\u672c\u4e2d\u662f\u7b2c6\u4e2a\u5b57\u7b26\u3002

3\u3001\u77e5\u8bc6\u70b9\u8bf4\u660e\uff1a
FIND()\u51fd\u6570\u67e5\u627e\u7b2c\u4e00\u53c2\u6570\u5728\u7b2c\u4e8c\u53c2\u6570\u4e2d\u7684\u4f4d\u7f6e\u3002\u5982\u4e0b\u56fe\uff0c\u67e5\u627e\u201cB\u201d\u5728\u201cABCD\u201d\u4e2d\u662f\u7b2c\u51e0\u4e2a\u5b57\u7b26\u3002\u7b2c\u4e00\u53c2\u6570\u662f\u8981\u67e5\u627e\u7684\u5b57\u7b26\u201cB\u201d\uff0c\u7b2c\u4e8c\u53c2\u6570\u662f\u88ab\u67e5\u627e\u7684\u5b57\u7b26\u4e32\u3002\u6700\u7ec8\u8fd4\u56de\u201cB\u201d\u5728\u201cABCD\u201d\u4e2d\u662f\u7b2c2\u4e2a\u5b57\u7b26\u3002


4\u3001\u7136\u540e\uff0c\u5728B2\u4e2d\u8f93\u5165\u516c\u5f0f\uff1a=MID(A2,FIND("#",A2)+1,99)
\u8fd9\u6837\uff0c\u5c31\u63d0\u53d6\u51fa\u4e86#\u540e\u7684\u5b57\u7b26\u3002


5\u3001\u77e5\u8bc6\u70b9\u8bf4\u660e\uff1aMID()\u51fd\u6570\u8fd4\u56de\u4ece\u5b57\u7b26\u4e32\u4e2d\u5236\u5b9a\u5b57\u7b26\u5f00\u59cb\u82e5\u5e72\u4e2a\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u3002\u5982\u4e0b\u56fe\uff0cMID()\u51fd\u6570\u8fd4\u56de\u201cABCDE\u201d\u5b57\u7b26\u4e32\u4e2d\u4ece\u7b2c2\u4e2a\u5b57\u7b26\u5f00\u59cb\u7684\u8fde\u7eed3\u4e2a\u5b57\u7b26\uff0c\u4e5f\u5c31\u662f\u8fd4\u56de\u201cBCD\u201d\u3002


6\u3001\u7efc\u4e0a\uff0c=MID(A2,FIND("#",A2)+1,99)\u7684\u610f\u601d\u5c31\u662f\u4eceA2\u5355\u5143\u683c#\u5b57\u7b26\u540e\u9762\u7684\u4e00\u4e2a\u5b57\u7b26\u8d77\uff0c\u53d6\u957f\u5ea6\u4e3a99\u7684\u5b57\u7b26\u4e32\u3002\u5176\u4e2d\u768499\u662f\u4e00\u4e2a\u8f83\u5927\u7684\u6570\u5b57\uff0c\u80fd\u6db5\u76d6#\u540e\u5b57\u7b26\u7684\u6700\u5927\u957f\u5ea6\u5373\u53ef\u3002

Python\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u548c\u5b50\u5b57\u7b26\u4e32
import re
re.findall("\d+",\u5b57\u7b26\u4e32)

没分..很简单 (python2.7)
s = "发表于:2013-06-04"
# 我猜你的方法是
filter(str.isalnum,s)
# result '20130604'

要提取时间的话,用正则表达式吧

使用正则表达式,过滤

我猜你是爬虫爬取到的吧,用分割比较方便,用split分割:,就可以了。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
str="发表于:2013-06-04"
import re
print re.search(r'\d{4}-\d{2}-\d{2}', str).group(0)


扩展阅读:python list ... python 判断 字符串 ... python readline ... python dataframe replace ... python endswith ... python 截取字符串 ... python 字符串 替换 ... python获取字符串的字母 ... python 字符串分割 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网