473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Re: Simple regular expression

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
1 1264
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: peterbe | last post by:
I want to match a word against a string such that 'peter' is found in "peter bengtsson" or " hey peter," or but in "thepeter bengtsson" or "hey peterbe," because the word has to stand on its own....
3
by: EFP | last post by:
Can anyone help me with a simple regular expression problem. All that I want to do is take a list of known data and extract a particular section of the string to form a new list. Here is my...
4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
11
by: Dimitris Georgakopuolos | last post by:
Hello, I have a text file that I load up to a string. The text includes certain expression like {firstName} or {userName} that I want to match and then replace with a new expression. However,...
18
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
4
by: drasko | last post by:
Hi all. I need to code simple and fast int regexp_match(char *regexp, char *string) function that will follow the expression regexp, and see if there is a matching in the string. If there is, it...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
1
by: NvrBst | last post by:
I want to use the .replace() method with the regular expression /^ %VAR % =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)" regular expression with "":...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.