python 正则表达式 groups和group有什么区别 什么是Python编程语言

Python\u7f16\u7a0b\u8bed\u8a00\u9002\u5408\u505a\u4ec0\u4e48\uff1f

Python\u8bed\u8a00\u901a\u4fd7\u6613\u61c2\u3001\u4e0a\u624b\u5feb\u3001\u8bed\u6cd5\u7b80\u5355\uff0c\u662f\u6bd4\u8f83\u70ed\u95e8\u7684\u884c\u4e1a\uff0c\u800c\u4e14Python\u8bed\u8a00\u53ef\u4ee5\u4ece\u4e8b\u7684\u5de5\u4f5c\u5c97\u4f4d\u6709\u5f88\u591a\uff0c\u6bd4\u5982\u8bf4\uff1a\u4eba\u5de5\u667a\u80fd\u3001\u6570\u636e\u5206\u6790\u3001\u79d1\u5b66\u8fd0\u7b97\u3001web\u5f00\u53d1\u3001\u722c\u866b\u3001\u673a\u5668\u5b66\u4e60\u7b49\uff0c\u9009\u62e9\u6027\u591a\uff0c\u85aa\u8d44\u5f85\u9047\u597d\uff0c\u5982\u679c\u5bf9Python\u611f\u5174\u8da3\u7684\u8bdd\uff0c\u53ef\u4ee5\u653e\u5fc3\u5b66\u4e60\u3002

\u4f18\u70b9\uff1aPython\u662f\u4e00\u79cd\u4ee3\u8868\u7b80\u5355\u4e3b\u4e49\u601d\u60f3\u7684\u8bed\u8a00\uff0c\u9605\u8bfb\u4e00\u4e2a\u826f\u597d\u7684Python\u7a0b\u5e8f\u5c31\u611f\u89c9\u50cf\u662f\u5728\u8bfb\u82f1\u8bed\u4e00\u6837\uff0c\u4f7f\u4f60\u80fd\u591f\u4e13\u6ce8\u4e8e\u89e3\u51b3\u95ee\u9898\u800c\u4e0d\u662f\u53bb\u641e\u660e\u767d\u8bed\u8a00\u672c\u8eab;
Python\u5e95\u5c42\u662f\u7528C\u8bed\u8a00\u7f16\u5199\u7684\uff0c\u5f88\u591a\u6807\u51c6\u5e93\u548c\u7b2c\u4e09\u65b9\u5e93\u4e5f\u90fd\u662f\u7528C\u5199\u7684\uff0c\u8fd0\u884c\u901f\u5ea6\u975e\u5e38\u5feb;
IPython
Notebook\u4f7f\u6211\u4eec\u66f4\u5bb9\u6613\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u5de5\u4f5c\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u4e0e\u540c\u65f6\u5171\u4eabNotebook\uff0c\u65e0\u9700\u4ed6\u4eec\u5b89\u88c5\u4efb\u4f55\u4e1c\u897f\uff0c\u5927\u5927\u51cf\u5c11\u7ec4\u7ec7\u4ee3\u7801\uff0c\u8f93\u51fa\u548c\u6ce8\u91ca\u6587\u4ef6\u7684\u5f00\u9500;
Python\u662f\u4e00\u79cd\u901a\u7528\u8bed\u8a00\uff0c\u5bb9\u6613\u548c\u76f4\u89c2\uff0c\u5728\u5b66\u4e60\u4e0a\u4f1a\u6bd4\u8f83\u5bb9\u6613\uff0c\u53ef\u4ee5\u52a0\u5feb\u4f60\u5199\u4e00\u4e2a\u7a0b\u5e8f\u7684\u5feb\u901f\uff0c\u6b64\u5916Python\u6d4b\u8bd5\u6846\u67b6\u662f\u4e00\u4e2a\u5185\u7f6e\u7684\uff0c\u53ef\u4ee5\u4fdd\u8bc1\u4f60\u7684\u4ee3\u7801\u662f\u53ef\u91cd\u590d\u4f7f\u7528\u548c\u53ef\u9760\u7684;
Python\u8fd8\u662f\u4e00\u4e2a\u591a\u7528\u9014\u8bed\u8a00\uff0c\u628a\u4e0d\u540c\u80cc\u666f\u7684\u4eba\u7ed3\u5408\u5728\u4e00\u8d77\uff0c\u4f5c\u4e3a\u4e00\u79cd\u5e38\u89c1\u7684\u3001\u5bb9\u6613\u7406\u89e3\uff0c\u5927\u90e8\u5206\u7a0b\u5e8f\u5458\u90fd\u61c2\uff0c\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u548c\u7edf\u8ba1\u5b66\u5bb6\u6c9f\u901a\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u7b80\u5355\u7684\u5de5\u5177\u5c31\u628a\u4f60\u6bcf\u4e00\u4e2a\u5de5\u4f5c\u4f19\u4f34\u6574\u5408\u8d77\u6765\u3002
\u7531\u4e8e\u5b83\u7684\u5f00\u6e90\u672c\u8d28\uff0cPython\u5df2\u7ecf\u88ab\u79fb\u690d\u5728\u8bb8\u591a\u5e73\u53f0\u4e0a\uff0c\u6bd4\u5982\u8bf4\uff1aLinux\u3001Windows\u3001FreeBSD\u3001Macintosh\u3001Solaris\u3001OS/2\u3001Amiga\u3001AROS\u7b49\u3002
\u5f88\u591a\u65f6\u5019\u4e0d\u80fd\u5c06\u7a0b\u5e8f\u8fde\u5199\u6210\u4e00\u884c\uff0c\u5982import sys;for i in sys.path:print
i\uff0c\u800cperl\u548cawk\u5c31\u65e0\u6b64\u9650\u5236\uff0c\u53ef\u4ee5\u8f83\u4e3a\u65b9\u4fbf\u7684\u5728shell\u4e0b\u5b8c\u6210\u7b80\u5355\u7a0b\u5e8f\uff0c\u4e0d\u9700\u8981\u5982Python\u4e00\u6837\uff0c\u5fc5\u987b\u5c06\u7a0b\u5e8f\u5199\u5165\u4e00\u4e2a.py\u6587\u4ef6;
Python\u8bed\u6cd5\u72ec\u7279\uff0c\u4e5f\u8bb8\u4e0d\u5e94\u8be5\u88ab\u79f0\u4e3a\u5c40\u9650\uff0c\u4f46\u662f\u5b83\u7528\u7f29\u8fdb\u6765\u533a\u5206\u8bed\u53e5\u5173\u7cfb\u7684\u65b9\u5f0f\u8fd8\u662f\u7ed9\u5f88\u591a\u521d\u5b66\u8005\u5e26\u6765\u4e86\u56f0\u60d1\uff0c\u5373\u4fbf\u662f\u5f88\u6709\u7ecf\u9a8c\u7684Python\u7a0b\u5e8f\u5458\uff0c\u4e5f\u53ef\u80fd\u9677\u5165\u9677\u9631;
Python\u5bf9\u6bd4C\u3001C++\u8bed\u8a00\u8fd0\u884c\u901f\u5ea6\u8981\u6162\u4e00\u4e9b\u3002

group和groups是两个不同的函数。

一般,m.group(N) 返回第N组括号匹配的字符。
而m.group() == m.group(0) == 所有匹配的字符,与括号无关,这个是API规定的。

m.groups() 返回所有括号匹配的字符,以tuple格式。
m.groups() == (m.group(0), m.group(1), ...)

对你给的例子:

m = re.match("([abc])+", "abc")
你的+号在括号外面。括号最多匹配到一个字符,要么是a, 要么是c,这个python引擎匹配的是末尾的c。
而m.group() == m.group(0) 这个返回的是整个匹配的字符串"abc".

关于捕获型括号在正则表达式里的用法,参见相关文档。

group和groups是两个不同的函数。

一般,m.group(N) 返回第N组括号匹配的字符。
而m.group() == m.group(0) == 所有匹配的字符,与括号无关,这个是API规定的。

m.groups() 返回所有括号匹配的字符,以tuple格式。
m.groups() == (m.group(0), m.group(1), ...)

正则表达式中,group()用来提取分组截获的字符串,()用来分组。

组是通过 "(" 和 ")" 元字符来标识的。 "(" 和 ")" 有很多在数学表达式中相同的意思;它们一起把在它们里面的表达式组成一组。举个例子,你可以用重复限制符,象 *, +, ?, 和 {m,n},来重复组里的内容,比如说(ab)* 将匹配零或更多个重复的 "ab"。

如果不引入括号,整个个表达式作为一个组,是group(0)



对于题目中的例子:

m = re.match("([abc])+", "abc")
+号在括号外面。括号最多匹配到一个字符,要么是a, 要么是c,这个python引擎匹配的是末尾的c。
而m.group() == m.group(0) 这个返回的是整个匹配的字符串"abc".



一、解释:  match()函数只检测RE是不是在string的开始位置匹配  search()会扫描整个string查找匹配,会扫描整个字符串并返回第一个成功的匹配  也就是说match()只有在0位置匹配成功的话才有返回,如果不是开始位置匹配成功的话,match()就返回none二、例子:  match():   print(re.match(‘super’, ‘superstition’).span())会返回(0, 5)   print(re.match(‘super’, ‘insuperable’))则返回None  search():   print(re.search(‘super’, ‘superstition’).span())返回(0, 5)   print(re.search(‘super’, ‘insuperable’).span())返回(2, 7)

结合所有回复,先上个我的例子作答:
import re
a = "I am a father, my age is 35. I like reading."
b = re.match('(I am).*?(\d+)', a, )
c = re.match('[Ii] am', a)
print(b.group())
print(b.group(0))
print(b.group(1))
print(b.group(2))
print(b.group(0,1,2))
print(b.groups())
#以下是对应的输出:
I am a father, my age is 35

I am a father, my age is 35 #可以看出group()默认参数是0,等于group(0),\
也即要匹配的字符串
I am #第1个括号的匹配

35 #第2个括号的匹配,表达式中有多少个括号,\
参数就最大是括号数
('I am a father, my age is 35', 'I am', '35')

('I am', '35') #groups()==group(1,2),是不包含group(0)的

楼主对正则的[]不理解,[abc]内只能匹配a,或b,或c。如果要匹配'abc',只能写成:
a = 'babccabc'

b = re.match('abc', a)

c = re.match(('abc'), a)

d = re.search('abc', a)

e = re.search(('abc'), a)

print(b) #match()只能匹配字符串开始处,匹配不上返回None

print(c) #可以看出一个匹配内容,可以有无小括号

print(d.group(0))

print(e.group())
#以下为输出
None
None
abc
abc

经多次测试,楼主的表达式含‘+’,会导致往后匹配,相当于贪婪模式,但是最后的数据确是取最后一位字母,而不是第一位,这点目前还不能给你个合理的解释

m.groups返回一个包含所有字符的元组,而m.group返回一个包含那些组所对应值的元组

扩展阅读:c++和python先学哪个 ... python写数学表达式 ... python代码大全 ... python表达式的书写规则 ... python爬虫的正则表达式 ... python正则表达式findall ... python 正则表达式 match ... 20个常用的正则表达式 ... python正则表达式菜鸟教程 ...

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