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

Exact match with regular expression

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.