> I have never done much with regular expressions, and I now find myself
> struggling with something simple. I am using the new re module (as
> opposed to the regex module). I am trying to match a string that does
> not contain a period. According to the documentation, the pattern
> '[^.]' should work, but it does not. If I use '[.]', it matches all
> strings that contain a period, but I cannot seem to get the complement
> to work. Am I missing something simple?
Well, just follow this explanation ...
The `[.]' pattern does this:
Match any string which contains at least one period.
The `[^.]' pattern does this:
Match any string which contains at least one non-period.
In other words, `[.]' and `[^.]' are not really complements.
Given some sample strings, the following table shows how they will be
matched given these two regular expressions, assuming that you're
always using re.search() to do the matching:
String Matches `[.]' Matches `[^.]'
------ ------------- --------------
Hello. Yes Yes
Hello No Yes
. Yes No
Here's one possible regular expression that will match a string that
has no periods:
Here's what this regular expression means:
Match any string which has zero or more occurrences of a string
of non-periods that stretches completely from the beginning of the
string to the end. The `^' at the left and the `$' at the right
are necessary to show that you are looking at the entire string,
from the leftmost character to the rightmost character; the `*'
is necessary to show that you want to match any number of non-periods,
In other words, `^[^.]$' is the complement of `[.]' ... at least when
Another way to handle this case is to use the `[.]' regular
expression, but to only look at strings which *don't* match it. In
if not re.search('[.]', string):
print '%s contains no periods' % (string)
Note the `not' in the `if' statement. This is interpreted as follows:
If it is *not* true that `string' contains at least one period,
then print the message.
I hope this helps. Good luck!
if "." not in string:
print string, "contains no periods"
"Some people, when confronted with a problem, think 'I know, I'll use
regular expressions.' Now they have two problems."
-- Jamie Zawinski, on comp.lang.emacs
> >Another way to handle this case is to use the `[.]' regular
> >expression, but to only look at strings which *don't* match it. In
> >other words:
> > if not re.search('[.]', string):
> > print '%s contains no periods' % (string)
> How about:
> if "." not in string:
> print string, "contains no periods"
Yes, that's another approach that will work, too ... although the
original poster was specifically asking for help in using a regular
Notice the backslash before the period. This is how
you unescape the meaning of a METACHARACTER.
| I'm not a regular expression guru, but I believe that the period is
| a METACHARACTER that matches any character.
Correct, except in -groups where it matches itself. This is because
[.], [.a-z] and . all have the same meaning, so it's pointless to make
it difficult to use . in -groups.
"These are, as I began, cumbersome ways / to kill a man. Simpler, direct,
and much more neat / is to see that he is living somewhere in the middle /
of the twentieth century, and leave him there." -- Edwin Brock