goku
goku

Reputation: 47

Python regex for matching odd number of consecutive backslashes

I want to find all odd number of continuous backslashes.

I tried the following expression:

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

However, it matches a single backslash if present.

Upvotes: 1

Views: 1037

Answers (3)

user557597
user557597

Reputation:

If you're looking for speed, this benchmarks the best:
\\(?<!\\\\)(?:\\\\)*(?!\\)

Regex1:   \\(?<!\\\\)(?:\\\\)*(?!\\)
Options:  < none >
Completed iterations:   50  /  50     ( x 1000 )
Matches found per iteration:   3
Elapsed Time:    0.23 s,   229.23 ms,   229231 µs

Regex2:   (?<!\\)\\(?:\\{2})*(?!\\)
Options:  < none >
Completed iterations:   50  /  50     ( x 1000 )
Matches found per iteration:   3
Elapsed Time:    0.83 s,   829.68 ms,   829684 µs

Regex3:   (?<!\\)(?:\\{2})*\\(?!\\)
Options:  < none >
Completed iterations:   50  /  50     ( x 1000 )
Matches found per iteration:   3
Elapsed Time:    1.09 s,   1086.34 ms,   1086340 µs

Upvotes: 0

Mustofa Rizwan
Mustofa Rizwan

Reputation: 10476

You can try this too:

(?<!\\)(?:\\{2})*\\(?!\\)

Explanation

  1. (?<!\\) No backslash in the back from the current location
  2. (?:\\{2}) matches each next consecutive pairs of backslash , zero or more occasion
  3. \\ matches a single backslash
  4. (?!\\) then checks if there is no more backslash immediately

Upvotes: 3

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627263

You may use

r'(?<!\\)\\(?:\\{2})*(?!\\)'

See the regex demo.

Details:

  • (?<!\\) - no backslash can appear right before the current location
  • \\ - a single backslash
  • (?:\\{2})* - 0 or more occurrences of 2 backslashes
  • (?!\\) - no \ can appear immediately to the right at the current location.

Note that in Python it is a good idea to use raw string literal when using literal backslashes in a regex pattern (to reduce the number of escapes and make the pattern look tidier, and avoid any issues related to misinterpreted backslashes.)

Upvotes: 1

Related Questions