'print all matches in a string python

i want to print all matches in a specific variable

code :

import re
regex = r"https?:\/\/?www[.]?telegram|t\.me\/[a-zA-Z0-9_-]*\/?$"
data="https://t.me/cronosapesd fdsfdsfds https://t.me/osapesdd fd"
matches = re.findall(regex, data)
print(matches)

This currently outputs []. I want the script to show "https://t.me/cronosapesd" and "https://t.me/osapesdd" only in a list

thanks !



Solution 1:[1]

The core of your regex t|telegram is not bound to a group, so it does not work as you expect, same for the $ at the end.

This one should work better: https?://(?:www\.)?t(?:elegram)?.me/[a-zA-Z0-9_-]*/?.

It starts same as yours, matches http or https, then www. maybe, then t and maybe elegram, then .me and the end of the url.

import re
regex = r"https?://(?:www\.)?t(?:elegram)?.me/[a-zA-Z0-9_-]*/?"
data="https://t.me/cronosapesd fdsfdsfds https://t.me/osapesdd fd"
matches = re.findall(regex, data)
print(matches)

And a demo with all the versions: https://regex101.com/r/LibrFn/1

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1