Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[(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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading