fork download
  1. # Python convertir un string en CamelCase a kebab-case (separados por guiones)
  2. # http://es.stackoverflow.com/a/61561/127
  3.  
  4. import re
  5.  
  6. pascal = re.compile(r"[A-Z]\d*(?:[A-Z\d]*(?=[A-Z]|$)|[a-z])")
  7.  
  8. def pascal_kebab(cadena):
  9. return pascal.sub(lambda m: ("-" if m.start() else "") + m.group().lower(), cadena)
  10.  
  11.  
  12.  
  13. pruebas = [
  14. 'VerHTMLDePag',
  15. 'Ver2HTMLDePag',
  16. 'Ver2HTMLPag2Info',
  17. 'HTMLFomatoPag',
  18. 'HTMLConXML',
  19. 'HTML5FomatoPag',
  20. 'HTML5ConXML',
  21. 'HTML5ConCSS3',
  22. 'HTML',
  23. 'VerQ',
  24. 'A2BFormato',
  25. 'Formato',
  26. 'SFormato'
  27. ]
  28.  
  29. for prueba in pruebas:
  30. print("%-16s => %s" % (prueba, pascal_kebab(prueba)))
Success #stdin #stdout 0.01s 28384KB
stdin
Standard input is empty
stdout
VerHTMLDePag     => ver-html-de-pag
Ver2HTMLDePag    => ver2-html-de-pag
Ver2HTMLPag2Info => ver2-html-pag2-info
HTMLFomatoPag    => html-fomato-pag
HTMLConXML       => html-con-xml
HTML5FomatoPag   => html5-fomato-pag
HTML5ConXML      => html5-con-xml
HTML5ConCSS3     => html5-con-css3
HTML             => html
VerQ             => ver-q
A2BFormato       => a2b-formato
Formato          => formato
SFormato         => s-formato