2

How can I count the max amount of consecutive string groups in a string?

import re

s = "HELLOasdHELLOasdHELLOHELLOHELLOasdHELLOHELLO"

# Give me the max amount of consecutive HELLO groups ---> wich is 3
# There's a group of 3 and a group of 2, but 3 is the max.

count = re.findall("(HELLO)+", s)        # count is: ['HELLO', 'HELLO', 'HELLO', 'HELLO']
count = len(count)
print(count)

Output is:

4

Which is totally wrong. The max amount of consecutive HELLO is 3.

I think I'm using the wrong RE and I have no clue how to count those repetitions in order to find the max.

And I can't understand why the output is 4.

Thanks!

0

3 Answers 3

2

You need to capture the entire string of consecutive HELLOs in your match; then you can work out the number of HELLOs by dividing the length of the match string by 5 (the length of HELLO). Using a list comprehension:

import re

s = "HELLOasdHELLOasdHELLOHELLOHELLOasdHELLOHELLO"

print(max([len(x) // 5 for x in re.findall(r'((?:HELLO)+)', s)]))

Output

3
0

I think you should change to another solution that is easier to understand than looking for short code.

s = "HELLOasdHELLOasdHELLOHELLOHELLOasdHELLOHELLO"

word_search = "HELLO"


def find_char(str_var: str, word_search: str) -> int:
    count = 0
    for i in range(len(s)):
        char = word_search * i
        if str_var.find(char) != - 1:
            count = i
    return count


find = find_char(s)
print(find) # 3

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Update:
Actually it can work with one line of code without requiring additional modules:

 c = max([i for i in range(len(s)) if s.find('HELLO' * i) != -1])
  • There are many cases where people use Regular Expressions to cool and reject simpler solutions to work.
  • Back to the topic of the topic owner. He doesn't understand why the output = 4.
  • Because re.findall () will return a list of elements that it finds, here, it finds 4 elements.
    And len (list) will return the total number of elements in a list -> output = 4.
  • with the regex of the above answers, it also found 4 elements and when assembled into the top code, len(list) = 4.
  • We need to explain that len​​(list) is not right.
  • The problem is in the form of find max(x) if x * 'HELLO' in s.
  • Regex is a powerful and useful tool! but we don't always use it.
1
  • I liked this solution without using regex. It is clear indeed. I didn't thought about using the find method. Thank you! Jan 15, 2020 at 18:02
0

As explained in this question: Regex behaving weird when finding floating point strings

If one or more groups are present in the pattern, [re.findall will] return a list of groups

Therefore you want a non-capturing group instead. Let's work through with the sample string and pattern:

s = 'HELLOasdHELLOasdHELLOHELLOHELLOasdHELLOHELLO'
p = 'HELLO'

To find all occurrences of consecutive repetitions of the pattern, we just need to modify your regex slight to use a non-capturing group:

>>> matches = re.findall(f'(?:{p})+', s)
>>> matches
['HELLO', 'HELLO', 'HELLOHELLOHELLO', 'HELLOHELLO']

Now we just need to find the longest string and divide its length by the length of the pattern:

>> max(map(len, matches)) // len(p)
3
2
  • sorry, the original post allowed two different interpretations. not sure which one is correct.
    – lenik
    Jan 15, 2020 at 6:56
  • Thank you! matches = re.findall('(?:{p})+', s) I'd put the f for format before the first ' to ensure that python knows that it has to replace p. That way it worked! Jan 15, 2020 at 17:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.