Advertisement
here2share

# regex_snake.py

Jul 19th, 2022
1,013
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.43 KB | None | 0 0
  1. # regex_snake.py
  2.  
  3. from re import sub
  4.  
  5. def snake(s):
  6.   return '_'.join(
  7.     sub('([A-Z][a-z]+)', r' \1',
  8.     sub('([A-Z]+)', r' \1',
  9.     s.replace('-', ' '))).split()).lower()
  10. '''
  11. snake('camelCase') # 'camel_case'
  12. snake('some text') # 'some_text'
  13. snake('some-mixed_string With spaces_underscores-and-hyphens')
  14. # 'some_mixed_string_with_spaces_underscores_and_hyphens'
  15. snake('AllThe-small Things') # 'all_the_small_things'
  16. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement