regex question

Malcolm Tredinnick malcolmt at smart.net.au
Thu Jan 20 22:46:39 EST 2000


On Thu, Jan 20, 2000 at 09:17:26PM -0500, Roy Smith wrote:
> This isn't really a python question per-se, but it came up while using 
> the re module, so there is a connection :-)

Yes it is, don't deny it. It's ok ... you can admit you use Python here. :-)

> 
> Let's say I've got a pattern "[0-9]", i.e. a 1-digit number.  I want to 
> search a string for that pattern, and only return a match if it's found 
> preceeded by either a non-digit or the beginning of the string, and 
> likewise followed by either a non-digit or the end of the string.  Is 
> there a single compact regex I can write to cover that?

One method would be to use the (?!...) construct, which matches the preceeding
reg-exp if it's not followed by what's in the brackets. So you could try:

	[0-9](?![0-9])|[0-9]$

(The second alternative is necessary because the (?!...) needs to match
something. At least, I *think* this is true and I don't have Python on my
current machine to test it. Hmm .. have to remedy that problem.)

> The best I can come up with would be something like:
> 
> (^[0-9][^0-9])|([^0-9][0-9][^0-9])|([^0-9][0-9]$)|(^[0-9]$)

Ugh!

> This uglyness is exaccerbated by the fact that 
> I'm not really looking for 1-digit strings, I'm looking for 12-digit hex 
> numbers (i.e. ethernet MAC addresses), where the basic pattern is:
> 
> [0-9a-fA-f][0-9a-fA-f][0-9a-fA-f][0-9a-fA-f][0-9a-fA-f][0-9a-fA-f][0-9a-f
> A-f][0-9a-fA-f][0-9a-fA-f][0-9a-fA-f][0-9a-fA-f][0-9a-fA-f].

I'm not 100% up to speed on MAC addresses, but if they really are just 12
digit hex strings, why not use:
	[0-9a-fA-F]{12, 12}
as your pattern?

If you want to save each digit in a group, put parentheses around the [...]
bit. It can also probably be improved by just using case-insensitive matching
(if you are just matching this expression). When you compile the pattern, add
the re.I flag to it.

Hope this helps, or at least, doesn't hinder too much.

Cheers,
Malcolm Tredinnick

--
Eagles may soar, but weasels don't get sucked into jet engines.





More information about the Python-list mailing list