qhnw.net
当前位置:首页 >> python正则表达式提取数字 >>

python正则表达式提取数字

>>> import re>>> str1 = 'balance-rr 0'>>> mode = re.compile(r'\d+')>>> >>> mode.findall(str1)['0']>>> str1 = '12j33jk12 ksdjfkj23jk4h1k23h'>>> mode.findall(str1)['12', '33', '12', '23', '4', '1', '23']>>>

reg=/[2_ u+47563137+34037717+00005324m+0063611d107_*l1++]/g;string result=reg.exec(把上面那段字符串贴进来);这样就提取出来了.然后你可以用string的split方法进行分割string arr[]=result.split(/+/);//返回一个数组

python用正则表达式提取字符串中的数字和子字符串import rere.findall("\d+",字符串)

>>> src = ur""" 8.5 """>>> src u'\n 8.5 \n'>>> import re>>> patt = re.compile(r"(\d+\.\d+)")>>> patt.findall(src) [u'8.5']>>>

注意空匹配也是匹配.*表示0或多匹配,一开头就有空匹配所以search马上完成,假使没找到的话search会返回none然后group()操作是会报错.你可以试试fianall()会从头找到尾,在找到666之前每个位置都是空匹配应改用\d+表1或多 import re temp

import restr = '$12,000'str = str.replace('[$,]','')

import res = 'speed=210,angle=150'm = re.findall(r'(\w*[0-9]+)\w*',s)print m结果:>>> import re>>> s = 'speed=210,angle=150'>>> m = re.findall(r'(\w*[0-9]+)\w*',s)>>> print m['210', '150']>>>

import rea = [列表]c = []for x in a: c.append(re.findall(r'\d+',x))

\(\d+\)

import res = '(272,252)(79,168)'pattern = re.compile('\((\d+),(\d+)\)')print pattern.findall(s)# [('272', '252'), ('79', '168')]

网站首页 | 网站地图
All rights reserved Powered by www.qhnw.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com