-1

I am using the Python re module.

I can use the regex r'\bA\b' (a raw string) to differentiate between 'A' and 'AA': it will find a match in the string 'A' and no matches in the string 'AA'.

I would like to achieve the same thing with a carat ^ instead of the A: I want a regex which differentiates between '^' and '^^'.

The problem I have is that the regex r'\b\^\b' does not find a match in '^'.

Any ideas?

Robert
  • 1,530
  • 1
  • 16
  • 24
  • You could probably use a [lookahead assertion](https://docs.python.org/2/howto/regex.html#lookahead-assertions). But as far as I'm aware, these are generally costly computationally, and there may be simpler ways to achieve your required results. –  Nov 26 '15 at 02:26
  • After a bit more searching I believe that this is more about the `\b` than about the `^`: answer http://stackoverflow.com/a/3241901/1243435 gives a good explanation of the *word boundary* symbol `\b`. – Robert Nov 26 '15 at 02:29

1 Answers1

2

You need to use lookaround for this:

(?<!\^)\^(?!\^)

\b is a word boundary, a place between a word character and a non-word character, so your pattern is quite non-specific (doesn't say anything about A specifically, A_ would also not match given that _ is a word character.

Here, we assert that there needs to be a place where the preceding character is not a caret, then a caret, then a place where the following character is not a caret (which boils down to "the caret must not be in caret company").

Amadan
  • 191,408
  • 23
  • 240
  • 301