Chinaunix

标题: 关于[和]的问题 [打印本页]

作者: 夏织风    时间: 2015-08-27 14:44
标题: 关于[和]的问题
match方法:match(string[,pos[,endpos]]),其中[和]指的是什么作用呢?
请问各位大神解释一下,万分感谢。
作者: yakczh_cu    时间: 2015-08-27 15:40
可有可无

有了search match就是多余的
作者: 夏织风    时间: 2015-08-27 15:46
确实感觉search方法比match要更强大。万分感谢
回复 2# yakczh_cu


   
作者: substr函数    时间: 2015-08-28 12:42
回复 3# 夏织风


LZ哪里更强大?
有没有哪种情况?
是用 search 方法比 match 要更强大的例子。

万分感谢!
作者: 夏织风    时间: 2015-08-28 14:19
# encoding: UTF-8
import re

# 将正则表达式编译成Pattern对象
pattern = re.compile(r'world')

# 使用search()查找匹配的子串,不存在能匹配的子串时将返回None
# 这个例子中使用match()无法成功匹配
match = pattern.search('hello world!')

if match:
    # 使用Match获得分组信息
    print match.group()

### 输出 ###
# world

回复 4# substr函数


   
作者: substr函数    时间: 2015-08-31 17:03
回复 5# 夏织风
谢谢大神。

大神。
这也是可以啊。

输出
world
  1. #!/usr/bin/python2
  2. # coding: utf-8

  3. import re
  4. s = 'hello world'
  5. p = re.compile('.*(world)')
  6. m = p.match(s)

  7. if m:
  8.     print m.group(1)

复制代码

作者: 夏织风    时间: 2015-09-06 09:46
我还只是个小白,算不上大神。
match适用的范围不如search,一般情况下,match就够用了,如果发现match不起作用,就不妨试试search
回复 6# substr函数


   




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2