夏织风 发表于 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#!/usr/bin/python2
# coding: utf-8

import re
s = 'hello world'
p = re.compile('.*(world)')
m = p.match(s)

if m:
    print m.group(1)

夏织风 发表于 2015-09-06 09:46

我还只是个小白,算不上大神。
match适用的范围不如search,一般情况下,match就够用了,如果发现match不起作用,就不妨试试search
回复 6# substr函数


   
页: [1]
查看完整版本: 关于[和]的问题