Last active
September 25, 2025 12:04
-
-
Save z-ai-lab/557b3b43af7eb94040ca091feed944ed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| def camel_to_snake(input_string): | |
| """ | |
| Convert camelCase or PascalCase strings to snake_case. | |
| Args: | |
| input_string (str): The input string in camelCase or PascalCase | |
| Returns: | |
| str: The converted string in snake_case | |
| """ | |
| # First regex: Insert underscore between lowercase and uppercase letters | |
| # Handles cases like 'camelCase' -> 'camel_Case' | |
| lowercase_to_uppercase = re.sub(r'([a-z])([A-Z])', r'\1_\2', input_string) | |
| # Second regex: Insert underscore between uppercase letter and uppercase+lowercase | |
| # Handles cases like 'XMLHttpRequest' -> 'XML_Http_Request' | |
| acronym_boundaries = re.sub(r'([A-Z])([A-Z][a-z])', r'\1_\2', lowercase_to_uppercase) | |
| # Convert to lowercase | |
| return acronym_boundaries.lower() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment