avatar Bite 280. Regular Expression Lookahead/Lookbehind

For this bite you are going to solve three different tasks, where each task becomes progressively more complicated. 

Use the re regular expression (RegEx) package from the beginning, as it should help you solve the final task.

For the purposes of this bite, a character refers to a unicode-character and is not limited to alphanumeric symbols!

Task 1

The goal of count_n_repetitions is to count how often characters are followed by themselves for n times. A repetition of a character refers to a sequence of the same character, which is not interrupted by a different character: AABA: A is repeated once after the first A.

Task 2

The second function, count_n_reps_or_chars_following should count how often characters are repeated for n times, or followed by the given input character n times. If char is also followed by itself it only counts once.

Task 3

The final function, check_surrounding_chars, should count the number of times a character is surrounded by characters from the given surrounding_chars list.

Examples

To see a couple of example inputs and outputs, look at this document.

Error Handling

We assume all inputs use the correct types and valid values. You only need to focus on the regular expression. :)

Hint To quickly test out regular expression, websites like regex101 can help a lot (don't forget to set the RegEx flavor to Python).

Login and get coding
go back Advanced level
Bitecoin 4X

19 out of 21 users completed this Bite.
Will you be Pythonista #20 to crack this Bite?
Resolution time: ~115 min. (avg. submissions of 5-240 min.)
Pythonistas rate this Bite 5.75 on a 1-10 difficulty scale.
» Up for a challenge? 💪

We use Python 3.8