We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
it is just a way of building a string with concatenation (using '+'), and it has nothing to do with the question mark '?', for example:
list = 'abcdefg'
pattern = '['+list+']'
is equivalent to
pattern = '[' + 'abcdefg' + ']'
and is equivalent to
pattern = '[abcdefg]'
'?' in your example is a part of syntax for lookahead/lookbehind assertions (see below)
lookahead/lookbehind are collectively called lookaround and used as the boundary assertion mechanism:
positive lookahead means that you want your pattern to be followed by a specific character/expression, example:
vowels = 'aeiou'
match = re.findall(r'['+vowels+']{2,}(?=\d)', input)
that means: in your input you are looking for two or more vowels (your pattern: ['+vowels+']{2,}) that have to be followed by a digit (positive lookahead: (?=\d)):
input match
'eo3ppaao' -> ['eo']
it doesn't match 'aao' as it is not followed by a digit.
syntax for positive lookahead is demonstrated above: to the right of your pattern you insert the opening parenthesis followed by a question mark and an equals sign:
[pattern](?=
then you add a character/expression you want your pattern to be followed by and the closing parenthesis:
[pattern](?=[expression])
negative lookahead means that you want your pattern to be followed by anything but certanly not by a specific character/expression, example:
vowels = 'aeiou'
match = re.findall(r'['+vowels+']{2,}(?!\d)', input)
that means: in your input you are looking for two or more vowels (your pattern: ['+vowels+']{2,}) that have to be followed by anything but certanly not by a digit (negative lookahead: (?!\d)):
input match
'eo3ppaao' -> ['aao']
it matches 'aao' as it is not followed by a digit.
note that it will not match 'aa' or 'ao' because {2,} is greedy.
syntax for negative lookahead is demonstrated above: to the right of your pattern you insert the opening parenthesis followed by a question mark and an exclamation point:
[pattern](?!
then you add a character/expression you want your pattern not to be followed by and the closing parenthesis:
[pattern](?![expression])
also note that using simple negation [^\d] instead of negative lookahead(?!\d) will not match anything if your pattern contains the last character of input as [^\d] means any character other than a digit but it still has to be a character. this is an important feature of lookaround's:
everything above goes for positive lookbehind and negative lookbehind in the same way. the only difference is that you are looking at the character/expression that is in front of your pattern, not after your pattern.
so, instead of inserting lookaround to the right of your pattern you insert lookaround to the left of it.
basically, it is the same as for lookahead, you just have to add '<' to the right of the question mark.
another important feature of lookaround's: they are excluded from the match.
for instance, in your example:
match = re.findall(r'(?<=['+consonants+'])(['+vowels+']{2,})(?=['+consonants+'])',raw_input(),flags = re.I)
if you find two or more vowels(your pattern) between consonants, match will return only vowels, lookaround's will be dropped, they are used only to assert boundaries of your pattern.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Re.findall() & Re.finditer()
You are viewing a single comment's thread. Return to all comments →
regarding ['+xyz+']:
it is just a way of building a string with concatenation (using '+'), and it has nothing to do with the question mark '?', for example:
is equivalent to
and is equivalent to
'?' in your example is a part of syntax for lookahead/lookbehind assertions (see below)
lookahead/lookbehind are collectively called lookaround and used as the boundary assertion mechanism:
positive lookahead means that you want your pattern to be followed by a specific character/expression, example:
that means: in your input you are looking for two or more vowels (your pattern: ['+vowels+']{2,}) that have to be followed by a digit (positive lookahead: (?=\d)):
it doesn't match 'aao' as it is not followed by a digit.
syntax for positive lookahead is demonstrated above: to the right of your pattern you insert the opening parenthesis followed by a question mark and an equals sign:
then you add a character/expression you want your pattern to be followed by and the closing parenthesis:
negative lookahead means that you want your pattern to be followed by anything but certanly not by a specific character/expression, example:
that means: in your input you are looking for two or more vowels (your pattern: ['+vowels+']{2,}) that have to be followed by anything but certanly not by a digit (negative lookahead: (?!\d)):
it matches 'aao' as it is not followed by a digit.
note that it will not match 'aa' or 'ao' because {2,} is greedy.
syntax for negative lookahead is demonstrated above: to the right of your pattern you insert the opening parenthesis followed by a question mark and an exclamation point:
then you add a character/expression you want your pattern not to be followed by and the closing parenthesis:
also note that using simple negation [^\d] instead of negative lookahead (?!\d) will not match anything if your pattern contains the last character of input as [^\d] means any character other than a digit but it still has to be a character. this is an important feature of lookaround's:
everything above goes for positive lookbehind and negative lookbehind in the same way. the only difference is that you are looking at the character/expression that is in front of your pattern, not after your pattern.
so, instead of inserting lookaround to the right of your pattern you insert lookaround to the left of it.
syntax for lookbehind changes to:
basically, it is the same as for lookahead, you just have to add '<' to the right of the question mark.
another important feature of lookaround's: they are excluded from the match.
for instance, in your example:
if you find two or more vowels(your pattern) between consonants, match will return only vowels, lookaround's will be dropped, they are used only to assert boundaries of your pattern.