0

I have made a regex with the findall and finditer methods, but they are different.
I need finditer to find matches like findall, and also need to know the span/indexes.

import re

string='This is my string.'

letters='abcdefghijklmnopqrstuvwxyzабвгґдеєжзиіїйклмнопрстуфхцчшщьюяыъэё'
letters+=letters.upper()+'\-\''

print(re.findall(rf'([{letters}]+?)(?:[^{letters}]|$)',string))
print(list(re.finditer(rf'([{letters}]+?)(?:[^{letters}]|$)',string)))

Output:
['This', 'is', 'my', 'string']
[<re.Match object; span=(0, 5), match='This '>, <re.Match object; span=(5, 8), match='is '>, <re.Match object; span=(8, 11), match='my '>, <re.Match object; span=(11, 17), match='string'>]
Igor Dragushhak
  • 448
  • 3
  • 13

0 Answers0