0

I'm trying to understand what the repeated (?i) syntax means in the following code:

i = self.expect([
  "(?i)are you sure you want to continue connecting",
  original_prompt,
  "(?i)(?:password)|(?:passphrase for key)",
  "(?i)permission denied",
  "(?i)terminal type",
  TIMEOUT,
  "(?i)connection closed by remote host"
], timeout=login_timeout)
pynexj
  • 19,215
  • 5
  • 38
  • 56

1 Answers1

1

This is documented under the heading for (?aiLmsux-imsx:...) in https://docs.python.org/3/library/re.html, as follows:

(?aiLmsux-imsx:...)

(Zero or more letters from the set a, i, L, m, s, u, x, optionally followed by - followed by one or more letters from the i, m, s, x.) The letters set or remove the corresponding flags: re.A (ASCII-only matching), re.I (ignore case), re.L (locale dependent), re.M (multi-line), re.S (dot matches all), re.U (Unicode matching), and re.X (verbose), for the part of the expression. (The flags are described in Module Contents.)

Thus, (?i) is an inline version of the flag that is otherwise set as re.I, or re.IGNORECASE; it makes the match case-insensitive, such that permission denied could also be written Permission Denied or PERMISSION DENIED but would still be matched.

Community
  • 1
  • 1
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441