Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of '(?i)password' in python regular expression?

Tags:

python

regex

Pexpect can be used to automate tasks in python (does not need TCL to be installed). One of the simplest routines of this class is the 'run()' routine. It accepts a dictionary of expected question patterns as keys and the responses as values. For example

pexpect.run ('scp foo [email protected]:.', events={'(?i)password': mypassword})

I know that usually '?' is used to indicate 0 or 1 occurrences of previous literal in the string (for regular expressions that is). However, over here, this does not seem to be the meaning.

Can experts comment on what is it?

like image 734
user30019 Avatar asked Oct 21 '08 16:10

user30019


People also ask

What does ?= * Mean in regex?

is a positive lookahead, a type of zero-width assertion. What it's saying is that the captured match must be followed by whatever is within the parentheses but that part isn't captured. Your example means the match needs to be followed by zero or more characters and then a digit (but again that part isn't captured).

What does .*? Mean in regex?

(. *?) matches any character ( . ) any number of times ( * ), as few times as possible to make the regex match ( ? ). You'll get a match on any string, but you'll only capture a blank string because of the question mark.

What are regex symbols in Python?

A RegEx, or Regular Expression, is a sequence of characters that forms a search pattern. RegEx can be used to check if a string contains the specified search pattern.


2 Answers

https://docs.python.org/library/re.html#regular-expression-syntax

(?...) This is an extension notation (a "?" following a "(" is not meaningful otherwise). The first character after the "?" determines what the meaning and further syntax of the construct is. Extensions usually do not create a new group; (?P...) is the only exception to this rule. Following are the currently supported extensions.

(?iLmsux) (One or more letters from the set "i", "L", "m", "s", "u", "x".) The group matches the empty string; the letters set the corresponding flags (re.I, re.L, re.M, re.S, re.U, re.X) for the entire regular expression. This is useful if you wish to include the flags as part of the regular expression, instead of passing a flag argument to the compile() function.

Note that the (?x) flag changes how the expression is parsed. It should be used first in the expression string, or after one or more whitespace characters. If there are non-whitespace characters before the flag, the results are undefined.

So in this case the string is a regular expression, and is set to be case-insensitive.

like image 191
Douglas Leeder Avatar answered Sep 30 '22 19:09

Douglas Leeder


This is an extension in the regular expression syntax in the re module of Python. The "i" means "ignore case". This means a case insensitive search for "password" is done.

from https://docs.python.org/library/re.html#regular-expression-syntax

(?iLmsux) (One or more letters from the set "i", "L", "m", "s", "u", "x".) The group matches the empty string; the letters set the corresponding flags (re.I, re.L, re.M, re.S, re.U, re.X) for the entire regular expression. This is useful if you wish to include the flags as part of the regular expression, instead of passing a flag argument to the compile() function.

Note that the (?x) flag changes how the expression is parsed. It should be used first in the expression string, or after one or more whitespace characters. If there are non-whitespace characters before the flag, the results are undefined.

like image 24
jakber Avatar answered Sep 30 '22 19:09

jakber