简体   繁体   中英

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.

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

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.)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM