473,395 Members | 1,541 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,395 software developers and data experts.

Exact match with regular expression

Hi,
I'd like to use regular expressions to parse a string and accept only
valid strings. What I mean is the possibility to check if the whole
string matches the regex.

So if I have:
>>p = re.compile('a*b*')
I can match this: 'aaaaaabbb'
>>m = p.match('aaaaaabbb')
m.group()
'aaaaaabbb'

But I'd like to get None with this: 'aabDDDDb'
Instead it matches the first part:
>>m = p.match('aabDDDDb')
m.group()
'aab'

I know this is the expected behaviour, but I'm sure it should be
possible doing what I said.
Are there other methods I don't know about in the re module?

Thanks.
Oct 26 '08 #1
6 15169
Mr.SpOOn wrote in news:mailman.3069.1225039892.3487.python-
li**@python.org in comp.lang.python:
Hi,
I'd like to use regular expressions to parse a string and accept only
valid strings. What I mean is the possibility to check if the whole
string matches the regex.

So if I have:
>>>p = re.compile('a*b*')

I can match this: 'aaaaaabbb'
>>>m = p.match('aaaaaabbb')
m.group()
'aaaaaabbb'

But I'd like to get None with this: 'aabDDDDb'
Instead it matches the first part:
>>>m = p.match('aabDDDDb')
m.group()
'aab'
Read (and bookmark) this:

http://www.python.org/doc/2.5.2/lib/re-syntax.html

You want the 3rd item down about the "$" special character.
>>p = re.compile('a*b*$')
m = p.match('aabDDDDb')
m is None
True
>>m = p.match('aaaaaabbb')
m.group()
'aaaaaabbb'

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Oct 26 '08 #2
In message <Xn**********************************@216.196.109. 145>, Rob
Williscroft wrote:
Read (and bookmark) this:

http://www.python.org/doc/2.5.2/lib/re-syntax.html
Funny how you never get a thank-you when you tell people to RTFM.
Nov 1 '08 #3
On Fri, Oct 31, 2008 at 8:57 PM, Lawrence D'Oliveiro
<ld*@geek-central.gen.new_zealandwrote:
In message <Xn**********************************@216.196.109. 145>, Rob
Williscroft wrote:
>Read (and bookmark) this:

http://www.python.org/doc/2.5.2/lib/re-syntax.html

Funny how you never get a thank-you when you tell people to RTFM.
--
http://mail.python.org/mailman/listinfo/python-list

Hmmm. Kind of odd. Someone involved in the language they're having
trouble with wrote some documentation for the exact thing they're
asking for, you tell them where it is, and that doesn't deserve
thanks?

Something's wrong here. Well, it's about time for that which must be
read by everyone on the Internet to be mentioned again.

If you haven't read this yet, read this:
http://www.catb.org/~esr/faqs/smart-questions.html

Especially if you have asked for help and not gotten what you wanted.
Especially if people ignore your questions.
Especially if people give answers that don't help you.
Nov 1 '08 #4
Lawrence D'Oliveiro wrote in news:ge**********@lust.ihug.co.nz in
comp.lang.python:
In message <Xn**********************************@216.196.109. 145>, Rob
Williscroft wrote:
>Read (and bookmark) this:

http://www.python.org/doc/2.5.2/lib/re-syntax.html

Funny how you never get a thank-you when you tell people to RTFM.
Saying "Thank You" is what email is for, which in this case is
what Mr.SpOOn did:
Read (and bookmark) this:

http://www.python.org/doc/2.5.2/lib/re-syntax.html

You want the 3rd item down about the "$" special character.

Great, thanks.
When I read about the symbol in the tutorial I didn't realize
what was it for.
Rob.
--
http://www.victim-prime.dsl.pipex.com/
Nov 1 '08 #5
On Sat, Nov 1, 2008 at 1:57 AM, Lawrence D'Oliveiro
<ld*@geek-central.gen.new_zealandwrote:
In message <Xn**********************************@216.196.109. 145>, Rob
Williscroft wrote:
>Read (and bookmark) this:

http://www.python.org/doc/2.5.2/lib/re-syntax.html

Funny how you never get a thank-you when you tell people to RTFM.
My fault :\
I said "thank you" to Rob, but I just sent a private message. It's
just that I did a reply and not reply to all.

Anyway, I RTFM and solved my problem :)

Thanks again,
Bye
Nov 3 '08 #6
In message <ma**************************************@python.o rg>, Mr.SpOOn
wrote:
On Sat, Nov 1, 2008 at 1:57 AM, Lawrence D'Oliveiro
<ld*@geek-central.gen.new_zealandwrote:
>Funny how you never get a thank-you when you tell people to RTFM.

My fault :\
I said "thank you" to Rob, but I just sent a private message. It's
just that I did a reply and not reply to all.
That's OK. My fault for not realizing. :)
Nov 4 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Ron Adam | last post by:
Is it possible to match a string to regular expression pattern instead of the other way around? For example, instead of finding a match within a string, I want to find out, (pass or fail), if...
0
by: Follower | last post by:
Hi, I am working on a function to return extracts from a text document with a specific phrase highlighted (i.e. display the context of the matched phrase). The requirements are: * Match...
1
by: Venkat | last post by:
Hi, I am using match function of string to find if a character is there in a string. The function Match is working fine with all the other characters except when the searching character is "+"....
19
by: Tom Deco | last post by:
Hi, I'm trying to use a regular expression to match a string containing a # (basically i'm looking for #include ...) I don't seem to manage to write a regular expression that matches this. ...
2
by: Christian Staffe | last post by:
Hi, I would like to check for a partial match between an input string and a regular expression using the Regex class in .NET. By partial match, I mean that the input string could not yet be...
1
by: tdmailbox | last post by:
I have the following regular expression. It works fine if the regex code returns a match. However if not the .match code fails. How can I code this so that it skips the match if the regular...
38
by: Steve Kirsch | last post by:
I need a simple function that can match the number of beginning and ending parenthesis in an expression. Here's a sample expression: ( ( "john" ) and ( "jane" ) and ( "joe" ) ) Does .NET have...
6
by: likong | last post by:
Hi, Any idea about how to write a regular expression that matches a substring xxx as long as the string does NOT contain substring yyy? Thanks. Kong
14
by: Andy B | last post by:
I need to create a regular expression that will match a 5 digit number, a space and then anything up to but not including the next closing html tag. Here is an example: <startTag>55555 any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...
0
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...

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.