How to declare a RegEx with character repetition to match the strings "http", "htttp", "httttp", etc in Python

1 Answer

0 votes
import re

# Declare the regex pattern
pattern = re.compile(r"htt+p")

# Test strings
test_strings = ["http", "htttp", "httttp", "httpp", "htp"]

# Check matches
for test in test_strings:
    match = bool(pattern.fullmatch(test))  # Use fullmatch for exact matches
    print(f'Matches "{test}": {match}')


# Matches "httpp": True or false, depending on how matches() method works


'''
run:

Matches "http": True
Matches "htttp": True
Matches "httttp": True
Matches "httpp": False
Matches "htp": False

'''

 



answered May 15, 2025 by avibootz
edited May 15, 2025 by avibootz
...