Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # regex_example.py
- import re
- def validate_string(text):
- """
- Validates if a string matches the required pattern.
- Args:
- text: The string to validate.
- Returns:
- True if the string matches the pattern, False otherwise.
- """
- pattern = r"^[a-zA-Z0-9][a-zA-Z0-9\-]{0,34}[a-zA-Z0-9]$"
- return bool(re.match(pattern, text))
- # Test Cases
- test_cases = [
- "abc123xyz",
- "a-1-b-2-c",
- "AbCd12",
- "A1",
- "Xyz123abc-123xyz",
- "longStringWith36Characters1234567890",
- "a-",
- "-abc",
- "abc!",
- "a",
- "abcde fghi",
- "longStringWith37Characters1234567890A"
- ]
- for test_case in test_cases:
- is_valid = validate_string(test_case)
- print(f"'{test_case}': {'Valid' if is_valid else 'Invalid'}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement