Reputation: 47
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
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
Reputation: 10476
You can try this too:
(?<!\\)(?:\\{2})*\\(?!\\)
(?<!\\)
No backslash in the back from the current location(?:\\{2})
matches each next consecutive pairs of backslash , zero
or more occasion\\
matches a single backslash(?!\\)
then checks if there is no more backslash immediatelyUpvotes: 3
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