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
  • 16,531
  • 5
  • 29
  • 48
  • 2
    BTW, a narrower question not asking for an explanation of the whole syntax but just of that one specific aspect would generally be better-received. I've taken the liberty of editing towards that end; if you have more questions, feel free to ask them separately. – Charles Duffy Mar 22 '19 at 20:45

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
  • 257,635
  • 38
  • 339
  • 400