Guest User

Untitled

a guest
Jan 17th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. >>> camel_case_split("CamelCaseXYZ")
  2. ['Camel', 'Case', 'XYZ']
  3. >>> camel_case_split("XYZCamelCase")
  4. ['XYZ', 'Camel', 'Case']
  5.  
  6. (?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])
  7.  
  8. >>> re.split("(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])", "CamelCaseXYZ")
  9. ['CamelCaseXYZ']
  10.  
  11. string: ''
  12. nrohwer: ['']
  13. casimir_et_hippolyte: []
  14. two_hundred_success: []
  15. kalefranz: string index out of range # with modification: either [] or ['']
  16.  
  17. string: ' '
  18. nrohwer: [' ']
  19. casimir_et_hippolyte: []
  20. two_hundred_success: [' ']
  21. kalefranz: [' ']
  22.  
  23. string: 'lower'
  24. all algorithms: ['lower']
  25.  
  26. string: 'UPPER'
  27. all algorithms: ['UPPER']
  28.  
  29. string: 'Initial'
  30. all algorithms: ['Initial']
  31.  
  32. string: 'dromedaryCase'
  33. nrohwer: ['dromedary', 'Case']
  34. casimir_et_hippolyte: ['dromedary', 'Case']
  35. two_hundred_success: ['dromedary', 'Case']
  36. kalefranz: ['Dromedary', 'Case'] # with modification: ['dromedary', 'Case']
  37.  
  38. string: 'CamelCase'
  39. all algorithms: ['Camel', 'Case']
  40.  
  41. string: 'ABCWordDEF'
  42. nrohwer: ['ABC', 'Word', 'DEF']
  43. casimir_et_hippolyte: ['ABC', 'Word', 'DEF']
  44. two_hundred_success: ['ABC', 'Word', 'DEF']
  45. kalefranz: ['ABCWord', 'DEF']
  46.  
  47. def camel_case_split(identifier):
  48. matches = finditer('.+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)', identifier)
  49. return [m.group(0) for m in matches]
  50.  
  51. import re
  52.  
  53. name = 'CamelCaseTest123'
  54. splitted = re.sub('(?!^)([A-Z][a-z]+)', r' 1', name).split()
  55.  
  56. ['Camel', 'Case', 'Test123']
  57.  
  58. re.findall(r'[A-Z](?:[a-z]+|[A-Z]*(?=[A-Z]|$))', 'CamelCaseXYZ')
  59.  
  60. ['Camel', 'Case', 'XYZ']
  61.  
  62. re.findall(r'[A-Z]?[a-z]+|[A-Z]+(?=[A-Z]|$)', 'camelCaseXYZ')
  63.  
  64. >>> re.findall("(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])", "CamelCaseXYZ")
  65. ['', '']
  66.  
  67. def camel_case_split(identifier):
  68. matches = finditer('(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])', identifier)
  69. split_string = []
  70. # index of beginning of slice
  71. previous = 0
  72. for match in matches:
  73. # get slice
  74. split_string.append(identifier[previous:match.start()])
  75. # advance index
  76. previous = match.start()
  77. # get remaining string
  78. split_string.append(identifier[previous:])
  79. return split_string
  80.  
  81. def camel_case_split(string):
  82. bldrs = [[string[0].upper()]]
  83. for c in string[1:]:
  84. if bldrs[-1][-1].islower() and c.isupper():
  85. bldrs.append([c])
  86. else:
  87. bldrs[-1].append(c)
  88. return [''.join(bldr) for bldr in bldrs]
  89.  
  90. def camel_case_split2(string):
  91. # set the logic for creating a "break"
  92. def is_transition(c1, c2):
  93. return c1.islower() and c2.isupper()
  94.  
  95. # start the builder list with the first character
  96. # enforce upper case
  97. bldr = [string[0].upper()]
  98. for c in string[1:]:
  99. # get the last character in the last element in the builder
  100. # note that strings can be addressed just like lists
  101. previous_character = bldr[-1][-1]
  102. if is_transition(previous_character, c):
  103. # start a new element in the list
  104. bldr.append(c)
  105. else:
  106. # append the character to the last string
  107. bldr[-1] += c
  108. return bldr
  109.  
  110. RE_WORDS = re.compile(r'''
  111. # Find words in a string. Order matters!
  112. [A-Z]+(?=[A-Z][a-z]) | # All upper case before a capitalized word
  113. [A-Z]?[a-z]+ | # Capitalized words / all lower case
  114. [A-Z]+ | # All upper case
  115. d+ # Numbers
  116. ''', re.VERBOSE)
  117.  
  118. assert RE_WORDS.findall('FOOBar') == ['FOO', 'Bar']
Add Comment
Please, Sign In to add comment