Skip to content
Advertisement

Start and End Position of symbols in a string

I am trying to find the start and end position of _ in a string as list of tuples.

The code I used is

sentence = 'special events _______ ______ ___ _______ ____ _____ _______ ___________ brochure subscriptions ticket guide'
symbol = '_'

position = [(match.start(),match.end()) for match in re.finditer(symbol, sentence)]

For this the output obtained is

[(15, 16), (16, 17), (17, 18), (18, 19), (19, 20)..................]

How to get the start and end position of continuous located symbols as a list of tuple.

Advertisement

Answer

You should add the + quantifier. And since symbol could be a special symbol for the regular expression you might want to escape it with re.escape.

import re

sentence = 'special events _______ ______ ___ _______ ____ _____ _______ ___________ brochure subscriptions ticket guide'
symbol = '_'

needle = f'{re.escape(symbol)}+'
position = [(match.start(),match.end()) for match in re.finditer(needle, sentence)]
print(position)

The result is [(15, 22), (23, 29), (30, 33), (34, 41), (42, 46), (47, 52), (53, 60), (61, 72)].

Please be aware that end is the position after the match as stated in the documentation for Match.end.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement