Python - Regex 之 findall
谨记:我只提取我需要的字符串,其它的扔掉。扫描方向 从左至右
正则 re.findall 的简单用法(返回string中所有与pattern相匹配的全部字串,返回形式为数组)
>>> regular_v1 = re.findall(r"docs","https://docs.python.org/3/whatsnew/3.6.html") >>> print (regular_v1) ['docs'] >>> regular_v2 = re.findall(r"^https","https://docs.python.org/3/whatsnew/3.6.html") >>> print (regular_v2) ['https']
用$符号表示以html结尾的字符串返回,判断是否字符串结束的字符串
>>> regular_v3 = re.findall(r"html$","https://docs.python.org/3/whatsnew/3.6.html") >>> print (regular_v3) ['html']
>>> regular_v5 = re.findall(r"\d","https://docs.python.org/3/whatsnew/3.6.html") >>> print (regular_v5) ['3', '3', '6'] >>> regular_v6 = re.findall(r"\d\d\d","https://docs.python.org/3/whatsnew/3.6.html/1234") >>> print (regular_v6) ['123']
“W”在正则里面代表匹配除了字母与数字以外的特殊符号
>>> regular_v9 = re.findall(r"\W","https://docs.python.org/3/whatsnew/3.6.html") >>> print (regular_v9) [':', '/', '/', '.', '.', '/', '/', '/', '.', '.']
注意:与 search group 区别:
>>> s1 = 'Hello, this is Joey' >>> s2 = 'The first price is $9.90 and the second price is $100' >>> print(re.findall(r'\w+', s1)) ['Hello', 'this', 'is', 'Joey'] >>> print(re.findall(r'\d+', s2)) ['9', '90', '100'] >>> s2 = 'The first price is $9.90 and the second price is $100' >>> print(re.search(r'\d+', s2).group()) 9 >>> for m in re.finditer(r'\d+', s2): print(m.group()) 9 90 100
。
posted on 2018-07-10 09:37 TrustNature 阅读(120) 评论(0) 编辑 收藏 举报