473,799 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: Simple regular expression

On Jun 27, 11:05*am, "John Salerno" <johnj...@NOSPA Mgmail.comwrote :
"python_ent hu" <srhe...@gmail. comwrote in message

news:47******** *************** ***********@d45 g2000hsc.google groups.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('stri ng') equivalent to re.match ('^string')

Jun 27 '08 #1
1 1285
On Jun 28, 2:26 am, python_enthu <srhe...@gmail. comwrote:
On Jun 27, 11:05 am, "John Salerno" <johnj...@NOSPA Mgmail.comwrote :
"python_ent hu" <srhe...@gmail. comwrote in message
news:47******** *************** ***********@d45 g2000hsc.google groups.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('stri ng') equivalent to re.match ('^string')
No. Perhaps you meant "is re.search('^str ing') equivalent to
re.match('strin g')"?

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.compil e(r'y');t='x'*1 00000" "rx.match(t )"
1000000 loops, best of 3: 1.08 usec per loop

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

C:\junk>python -m timeit -s "import
re;rx=re.compil e(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.compil e(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
1612
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. The following code works for a single word: def createStandaloneWordRegex(word): """ return a regular expression that can find 'peter' only if it's written alone (next to space, start of string, end of string, comma, etc) but
3
2149
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 code snipet: my $fh = new FileHandle('c:\Units.txt') ; ## sucks up my work file my @lines1 = <$fh>; ## assigns the filehandler contents to @lines1 foreach $string (@lines1) ## extract each line from @lines and put it
4
3232
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 over each document, find out if it contains a header and/or a footer and extract only the main content part. The headers and the footers have no specific format and I have to detect and remove them using a list of strings that may appear as...
11
5395
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, I want to use the text included within the brackets to do a lookup so that I can replace the expression with the new text. For example:
18
3044
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
3831
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 want to avoid that. My question here is if there is a way to pass either a memory stream or array of "find", "replace" expressions or any other way to avoid multiple copies of a string. Any help will be highly appreciated
25
5174
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 (CONDUCTION DEFECT) 37.33/2 HEART (CONDUCTION DEFECT) WITH CATHETER 37.34/2 " the expression is "HEART (CONDUCTION DEFECT)". How do I gain access to the expression (not the matches) at runtime? Thanks, Mike
4
2484
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 will return 1 (TRUE). Are there some examples I can see, do you have ideas how to start with this, and so on... GNU C regexp is to big and complicated for me, it takes a lot of space (i need this for the use in embedded system). I'll...
1
4388
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 the first regular expression that matches the string. I've gor the regular expressions ordered so that the highest priority is first (if two or more regular expressions match the string I want the first one returned) The code that does this has...
1
3409
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 "": --------------------------------- myStringVar = myStringVar.replace("^" + iName + "=,($|&)", ""); --------------------------------- The following DOES replace it though: --------------------------------- var match = myStringVar.match("^" + iName + "=,($|&)");
0
9543
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10488
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10237
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10029
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4144
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.