Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Regex trouble

1 view
Skip to first unread message

MRAB

unread,
Sep 24, 2009, 11:25:05 AM9/24/09
to pytho...@python.org
Support Desk wrote:
> I am trying to loop over a dictionary of phone numbers and using a
> python regex to determine if they are long distance or local and then
> adding them to their appropriate dictionary, My regex doesn't appear to
> be working though.
>
> My regex's are these
>
> international__iregex=r'^1?(011|001)'
> local__iregex=r'^1?(281|832|713|800)'
> #long distance
> ld_regex=r'^1?(281|832|713|800|866|877|011|001|888)'
>
> long_distance= {}
> My loop:
> for key1,value1 in x.items():
> if key1 == 'dest':
> if re.search(ld_regex,value1):
> long_distance[key1] = value1
> print long_distance
>
Define "not working".

BTW, x.items() will give the key/value pairs, but key1 == 'dest' will be
true for only one of the keys, so you're really only looking at
x['dest'].

Please provide a simple though complete example showing the problem,
stating what you expected and what you actually got, including any
tracebacks if an exception occurred.

Simon Forman

unread,
Sep 24, 2009, 2:45:37 PM9/24/09
to Support Desk, Python
On Thu, Sep 24, 2009 at 10:43 AM, Support Desk <mi...@ipglobal.net> wrote:
> I am trying to loop over a dictionary  of phone numbers and using a python
> regex to determine if they are long distance or local and then adding them
> to their appropriate dictionary, My regex doesn't appear to be working
> though.

"doesn't appear to be working" is a useless way to characterize the
problem you're having.

Always tell us what you expected and what you got instead. Preferably
with the actual traceback if any.

Also, post a /runnable/ code fragment. That way we can try it out for ourselves.


FWIW this problem is too simple (IMHO) for regular expressions.
Simply carve off the first three digits and check against sets of the
prefixes you're interested in:


#any number starting with these prefixes is not long distance
local_prefixes = set(['832', '877', '888', '713', '866', '011', '001',
'281', '800'])

long_distance= {}
for key1, value1 in x.iteritems():
if key1 == 'dest':
if value1[:3] not in local_prefixes:
long_distance[key1] = value1

HTH,
~Simon

>
> My regex's are these
>
>
>

> international__iregex=r'^1?(011|001)'  #Anything starting with these
> prefixes is International
>
> local__iregex=r'^1?(281|832|713|800)' #anything starting with these are
> local
>
> #long distance
>
> ld_regex=r'^1?(281|832|713|800|866|877|011|001|888)'  #any number not
> starting with these prefixes is long distance


>
>
>
> long_distance= {}
>
> My loop:
>
>                 for key1,value1 in x.items():
>
>                         if key1 == 'dest':
>

>                                 if not re.search(ld_regex,value1):


>
>                                         long_distance[key1] = value1
>
>                                 print long_distance

Out of curiosity, why are you printing the whole dict for every "key1
== 'dest'" even when you're not modifying the output dict?

>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>

Rhodri James

unread,
Sep 24, 2009, 6:31:39 PM9/24/09
to Python
On Thu, 24 Sep 2009 19:45:37 +0100, Simon Forman <sajm...@gmail.com>
wrote:

> FWIW this problem is too simple (IMHO) for regular expressions.
> Simply carve off the first three digits and check against sets of the
> prefixes you're interested in:
>
>
> #any number starting with these prefixes is not long distance
> local_prefixes = set(['832', '877', '888', '713', '866', '011', '001',
> '281', '800'])
>
> long_distance= {}
> for key1, value1 in x.iteritems():
> if key1 == 'dest':
> if value1[:3] not in local_prefixes:
> long_distance[key1] = value1

You need to allow for the potential leading 1, which can be done with
a bit of straightforward string slicing but still puts REs more
sensibly in the running.

--
Rhodri James *-* Wildebeest Herder to the Masses

Simon Forman

unread,
Sep 24, 2009, 7:20:47 PM9/24/09
to Rhodri James, Python
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Wow, I was looking right at it ('1?') and I missed it. I need more sleep.


This is slightly less win:

for key1, value1 in x.iteritems():
if key1 == 'dest':

n = value1[0] == '1'
if value1[n:n + 3] not in local_prefixes:
long_distance[key1] = value1


(MRAB's comment about the foolishness of iterating through the dict
but testing for key == string_literal still applies, of course.)


Depending on what the OP's doing I might recommend just "normalizing"
the phone numbers before processing them, (i.e. stripping off the '1's
and possible breaking them up into tuples ('nnn', 'nnn', 'nnnn').

~Simon

0 new messages