Advertisement
here2share

# regex_example.py

Dec 23rd, 2024
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | None | 0 0
  1. # regex_example.py
  2.  
  3. import re
  4.  
  5. def validate_string(text):
  6.     """
  7.    Validates if a string matches the required pattern.
  8.  
  9.    Args:
  10.      text: The string to validate.
  11.  
  12.    Returns:
  13.      True if the string matches the pattern, False otherwise.
  14.    """
  15.     pattern = r"^[a-zA-Z0-9][a-zA-Z0-9\-]{0,34}[a-zA-Z0-9]$"
  16.     return bool(re.match(pattern, text))
  17.  
  18. # Test Cases
  19. test_cases = [
  20.     "abc123xyz",
  21.     "a-1-b-2-c",
  22.     "AbCd12",
  23.     "A1",
  24.      "Xyz123abc-123xyz",
  25.     "longStringWith36Characters1234567890",
  26.     "a-",
  27.     "-abc",
  28.     "abc!",
  29.     "a",
  30.     "abcde fghi",
  31.      "longStringWith37Characters1234567890A"
  32. ]
  33.  
  34. for test_case in test_cases:
  35.     is_valid = validate_string(test_case)
  36.     print(f"'{test_case}': {'Valid' if is_valid else 'Invalid'}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement