fork download
  1. import re
  2.  
  3. def convert(name):
  4. return re.sub(r'([A-Z]*)([A-Z][a-z]+)', lambda x: (x.group(1) + '_' if x.group(1) else '') + x.group(2) + '_', name).rstrip('_').lower()
  5.  
  6. def camel_to_snake(name):
  7. return re.sub(r"(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])", '_', name).lower()
  8.  
  9. print convert('CamelCase')
  10. print convert('HTTP')
  11. print convert('HTTPRequest')
  12. print convert('SecureHTTPRequest')
  13. print convert('SecureHTTP')
  14.  
  15. print camel_to_snake('CamelCase')
  16. print camel_to_snake('HTTP')
  17. print camel_to_snake('HTTPRequest')
  18. print camel_to_snake('SecureHTTPRequest')
  19. print camel_to_snake('SecureHTTP')
Success #stdin #stdout 0.03s 63700KB
stdin
Standard input is empty
stdout
camel_case
http
http_request
secure_http_request
secure_http
camel_case
http
http_request
secure_http_request
secure_http