Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old August 8th, 2003, 03:59 PM
ruach ruach is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 8 ruach User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
regular expressions and back references

Greetings, I am having a problem using back references in my regex and I am having a difficult time figuring out what I am doing wrong. My regex works fine with out the back refs but when I try to use them it won't match my sample. It looks to me that I am using them no differently then my examples and documentation but to no avail.

Here is my patteren:

macExpression = "^[0-9A-F]{1,2}(\:|\.|\-)[0-9A-F]{1,2}\1[0-9A-F]{1,2}\1[0-9A-F]{1,2}\1[0-9A-F]{1,2}\1[0-9A-F]{1,2}$:

And this is how I am using it:

matched = re.match(macExpression, macAddress)

I am trying to match mac addresses in the following formats 0:a0:c9:ee:b2:c0, 0-a0-c9-ee-b2-c0 & 0.a0.c9.ee.b2.c0 etc.

I wasn't sure how to do it but then I read about back references and I thought that all was well... Alas If any one could lend a hand I would appreciate it very much.

-matthew

Reply With Quote
  #2  
Old August 8th, 2003, 04:45 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Just a quick glance but, Have you tired it without escaping "1" - by back referancing i'm assuming you mean escaping special char's - since it isn't a special char so escaping it makes no sence. You said it works before you escape the chars so hopefully this will do it. Let me know if it doesn't.

Have fun,
Mark.

Reply With Quote
  #3  
Old August 8th, 2003, 04:52 PM
ruach ruach is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 8 ruach User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
back referencing explained

The 1 is supposed to be escaped. Escaping a number is what makes it a back reference rather then a literal number. The following quote from this site is where I first learned about back referencing. But it is also mentioned in Programing Python by Mark Lutz.
Quote:
One powerful option in creating search patterns is specifying that a subexpression that was matched earlier in a regular expression is matched again later in the expression. We do this using backreferences. Backreferences are named by the numbers 1 through 9, preceded by the backslash/escape character when used in this manner. These backreferences refer to each successive group in the match pattern, as in /(one)(two)(three)/\1\2\3/. Each numbered backreference refers to the group that, in this example, has the word corresponding to the number.


In short a back reference refers to a previously matched expresion by order of occurance.

-matthew

Reply With Quote
  #4  
Old August 8th, 2003, 06:44 PM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 512 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 29 sec
Reputation Power: 8
Just a stab in the dark here, not having investigated backreferences much, but why are you using them here? It seems like you put a backreference in if you expect the same text to crop up later in the regexp, but you're not expecting that. What's wrong with a "normal" regexp?

Reply With Quote
  #5  
Old August 8th, 2003, 07:11 PM
sacrilege sacrilege is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Norwich, UK
Posts: 53 sacrilege User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 1 m 53 sec
Reputation Power: 6
The problem is your escaping. When you use double quotes around a string in Python it will treat backslashes as an escape character. Meaning to actually put a literal '\' in your string means using '\\'. Simply replace all instances of '\' with '\\' or use single quotes around the expression string.

Reply With Quote
  #6  
Old August 8th, 2003, 07:59 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Hi, sorry i've never heard of or used backreferances so i assumed you ment escaping but I have to say that telex is right, why are you using backreferances when a simple regex would do the job.

Just make a regex to match the strings and use findall to get all the resulting matches..

Mark.

Reply With Quote
  #7  
Old August 9th, 2003, 04:44 AM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Another way to escape \ is to use a raw string, such as:
Code:
r'(?<=\n)\t\tdef ([\w\d_])\(.*?\):(?=\n)'


That will work just as well as using \\ notation, and looks cleaner.

Also, not knowing for what the RE is used, it seems as if a backreference is in actuality quite necessary here. If he matches '\-' in the text, he might not want to match '\:' later on, but just the '\-'. That's what back-references are for, it's not just repeating an earlier pattern, it's matching against an earlier match.

Last edited by percivall : August 9th, 2003 at 04:49 AM.

Reply With Quote
  #8  
Old August 9th, 2003, 07:35 AM
sacrilege sacrilege is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Norwich, UK
Posts: 53 sacrilege User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 1 m 53 sec
Reputation Power: 6
In addition to my other post you'll also want to set the IGNORECASE flag otherwise it will currently only match against uppercase letters. And percivall was right, you'll need to make it a raw string if you use single quotes.

So you might use your pattern like this:

Code:
rex = re.compile(r'^[0-9A-F]{1,2}(\:|\.|\-)[0-9A-F]{1,2}\1[0-9A-F]{1,2}\1[0-9A-F]{1,2}\1[0-9A-F]{1,2}\1[0-9A-F]{1,2}$', re.IGNORECASE)
matches = rex.match('0:a0:c9:ee:b2:c0')

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > regular expressions and back references


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway