By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,563 Members | 915 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,563 IT Pros & Developers. It's quick & easy.

Re: Simple regular expression

P: n/a
On Jun 27, 11:05*am, "John Salerno" <johnj...@NOSPAMgmail.comwrote:
"python_enthu" <srhe...@gmail.comwrote in message

news:47**********************************@d45g2000 hsc.googlegroups.com...
I am trying this.. what is wrong in this..
IDLE 1.2.2
>>import re
a="my name is fname lname"
p=re.compile('name')
m=p.match (a)
print p.match(a)
None

* * * match( string[, pos[, endpos]])

If zero or more characters at the beginning of string match this regular
expression, return a corresponding MatchObject instance. Return None if the
string does not match the pattern; note that this is different from a
zero-length match.

* * * search( string[, pos[, endpos]])

Scan through string looking for a location where this regular expression
produces a match, and return a corresponding MatchObject instance. Return
None if no position in the string matches the pattern; note that this is
different from finding a zero-length match at some point in the string.
Thanks John Salerno and John Machin,

I am used to perl. So I guess I am better of using re.search instead
of re.match
BTW, is re.search('string') equivalent to re.match ('^string')

Jun 27 '08 #1
Share this Question
Share on Google+
1 Reply

P: n/a
On Jun 28, 2:26 am, python_enthu <srhe...@gmail.comwrote:
On Jun 27, 11:05 am, "John Salerno" <johnj...@NOSPAMgmail.comwrote:
"python_enthu" <srhe...@gmail.comwrote in message
news:47**********************************@d45g2000 hsc.googlegroups.com...
>I am trying this.. what is wrong in this..
IDLE 1.2.2
>>>import re
>>>a="my name is fname lname"
>>>p=re.compile('name')
>>>m=p.match (a)
>>>print p.match(a)
None
match( string[, pos[, endpos]])
If zero or more characters at the beginning of string match this regular
expression, return a corresponding MatchObject instance. Return None if the
string does not match the pattern; note that this is different from a
zero-length match.
search( string[, pos[, endpos]])
Scan through string looking for a location where this regular expression
produces a match, and return a corresponding MatchObject instance. Return
None if no position in the string matches the pattern; note that this is
different from finding a zero-length match at some point in the string.

Thanks John Salerno and John Machin,

I am used to perl. So I guess I am better of using re.search instead
of re.match
BTW, is re.search('string') equivalent to re.match ('^string')
No. Perhaps you meant "is re.search('^string') equivalent to
re.match('string')"?

There are functional differences, which are documented in the section
of the manual to which I referred you.

There is currently (2.5.2) a speed penalty for not reading the manual:

C:\junk>python -m timeit -s "import
re;rx=re.compile(r'y');t='x'*100000" "rx.match(t)"
1000000 loops, best of 3: 1.08 usec per loop

C:\junk>python -m timeit -s "import
re;rx=re.compile(r'y');t='x'*10000000" "rx.match(t)"
1000000 loops, best of 3: 1.07 usec per loop

C:\junk>python -m timeit -s "import
re;rx=re.compile(r'^y');t='x'*100000" "rx.search(t)"
100 loops, best of 3: 2.76 msec per loop

C:\junk>python -m timeit -s "import
re;rx=re.compile(r'^y');t='x'*10000000" "rx.search(t)"
10 loops, best of 3: 277 msec per loop

.... and no queue of petitioners requesting an enhancement.

Cheers,
John
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.