Regular Expressions...

K

Ken D'Ambrosio

Hi, all. As a recovering Perl guy, I have to admit I don't quite "get"
the re module. For example, I'd like to do a few things (I'm going to use
phone numbers, 'cause that's what I'm currently dealing with):
12345678900 -- How would I:
- Get just the area code?
- Get just the seven-digit number?

In Perl, I'd so something like
m/^1(...)(.......)/;
and then I'd have that stuff in $1 and $2, respectively. But the Python
stuff
simply isn't clicking for me. If anyone could supply concrete examples of
how to do the problem, above, that would be terrific.

Thanks!

-Ken
 
J

James Stroud

Ken said:
Hi, all. As a recovering Perl guy, [snip]
In Perl, I'd so something like
m/^1(...)(.......)/;

Indeed it seems you are recovering from an especially bad case. I
recommend two doses of the python cookbook per day for one to two
months. Report back here after your first cycle and we'll tell you how
you are doing. I'm very optimistic about the prognosis.

James
 
J

James Mills

Indeed it seems you are recovering from an especially bad case. I recommend
two doses of the python cookbook per day for one to two months. Report back
here after your first cycle and we'll tell you how you are doing. I'm very
optimistic about the prognosis.

2nd opinion :)

I highly recommend a strong dose of the Python Tutorial (1)
followed by a recovery program of the Python Docs (2)

cheers
James

1. http://docs.python.org/tutorial/
2. http://docs.python.org/
 
B

bearophileHUGS

Ken D'Ambrosio:
 But the Python stuff simply isn't clicking for me.

For people coming from Perl that want to perform some string
processing with Python I suggest to learn first array/string slices
and string methods. And to try to use the regular expressions as
little as possible.

Bye,
bearophile
 
A

Aahz

Hi, all. As a recovering Perl guy, I have to admit I don't quite "get"
the re module.

Refer to the following every time you want to use regexes in Python:

'Some people, when confronted with a problem, think "I know, I'll use
regular expressions." Now they have two problems.'
--Jamie Zawinski, comp.emacs.xemacs, 8/1997

Although there are times when regexes are your best option, Python has
many other good options for processing strings, and your code readability
will usually increase if you try one of them first.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,145
Latest member
web3PRAgeency
Top