您的位置:首页 > 其它

Regular Expression Discussion

2015-06-08 12:08 363 查看
1. Pattern .*? will match all the text appended by for only once and return result:

Example:

pattern = r'[0-9].*?'
text = r'9800abcd'

match = re.findall(pattern, text)

Result:
match

Out[6]: ['9', '8', '0', '0']

2. Pattern .+? will match all the text appended by for two times and return result:
Example:
pattern = r'[0-9].+?'
text = r'9800abcd'

match = re.findall(pattern, text)

Result:
match

Out[8]: ['98', '00']

3. About the Matching Result of findall:

@1.当给出的正则表达式中带有多个括号时,列表的元素为多个字符串组成的tuple,tuple中字符串个数与括号对数相同,字符串内容与每个括号内的正则表达式相对应,并且排放顺序是按括号出现的顺序。

@2.当给出的正则表达式中带有一个括号时,列表的元素为字符串,此字符串的内容与括号中的正则表达式相对应(不是整个正则表达式的匹配内容)。

@3.当给出的正则表达式中不带括号时,列表的元素为字符串,此字符串为整个正则表达式匹配的内容。

quoted from : http://developer.51cto.com/art/201003/188828.htm
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: