regex question
Roy Smith
roy at popmail.med.nyu.edu
Thu Jan 20 21:17:26 EST 2000
This isn't really a python question per-se, but it came up while using
the re module, so there is a connection :-)
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?
For example:
pat = re.compile (pattern)
x = pat.search ("1")
y = pat.search ("12")
z = pat.search ("the number 1, don't you know?")
x and z should be MatchObjects, but y should be None.
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]$)
which is fairly ugly. 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].
More information about the Python-list
mailing list