Regular Expressions...

James Mills prologic at shortcircuit.net.au
Wed Jan 7 18:02:14 EST 2009


On Thu, Jan 8, 2009 at 8:54 AM, Ken D'Ambrosio <ken.dambrosio at segway.com> wrote:
> 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.

There is nothing so special or different about
Python's re module than say over any other language's
regular expression library or capabilities. You should
be able to use pretty much the same things, however:

1. Why can't you just use ordinary string manipulation her e?

One of Python's strengths is in string manipulation.

Consider:

>>> s = "1234567890"
>>> area, number = s[:2], s[2:]
>>> area
'12'
>>> number
'34567890'

cheers
James



More information about the Python-list mailing list