Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- >>> camel_case_split("CamelCaseXYZ")
- ['Camel', 'Case', 'XYZ']
- >>> camel_case_split("XYZCamelCase")
- ['XYZ', 'Camel', 'Case']
- (?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])
- >>> re.split("(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])", "CamelCaseXYZ")
- ['CamelCaseXYZ']
- string: ''
- nrohwer: ['']
- casimir_et_hippolyte: []
- two_hundred_success: []
- kalefranz: string index out of range # with modification: either [] or ['']
- string: ' '
- nrohwer: [' ']
- casimir_et_hippolyte: []
- two_hundred_success: [' ']
- kalefranz: [' ']
- string: 'lower'
- all algorithms: ['lower']
- string: 'UPPER'
- all algorithms: ['UPPER']
- string: 'Initial'
- all algorithms: ['Initial']
- string: 'dromedaryCase'
- nrohwer: ['dromedary', 'Case']
- casimir_et_hippolyte: ['dromedary', 'Case']
- two_hundred_success: ['dromedary', 'Case']
- kalefranz: ['Dromedary', 'Case'] # with modification: ['dromedary', 'Case']
- string: 'CamelCase'
- all algorithms: ['Camel', 'Case']
- string: 'ABCWordDEF'
- nrohwer: ['ABC', 'Word', 'DEF']
- casimir_et_hippolyte: ['ABC', 'Word', 'DEF']
- two_hundred_success: ['ABC', 'Word', 'DEF']
- kalefranz: ['ABCWord', 'DEF']
- def camel_case_split(identifier):
- matches = finditer('.+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)', identifier)
- return [m.group(0) for m in matches]
- import re
- name = 'CamelCaseTest123'
- splitted = re.sub('(?!^)([A-Z][a-z]+)', r' 1', name).split()
- ['Camel', 'Case', 'Test123']
- re.findall(r'[A-Z](?:[a-z]+|[A-Z]*(?=[A-Z]|$))', 'CamelCaseXYZ')
- ['Camel', 'Case', 'XYZ']
- re.findall(r'[A-Z]?[a-z]+|[A-Z]+(?=[A-Z]|$)', 'camelCaseXYZ')
- >>> re.findall("(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])", "CamelCaseXYZ")
- ['', '']
- def camel_case_split(identifier):
- matches = finditer('(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])', identifier)
- split_string = []
- # index of beginning of slice
- previous = 0
- for match in matches:
- # get slice
- split_string.append(identifier[previous:match.start()])
- # advance index
- previous = match.start()
- # get remaining string
- split_string.append(identifier[previous:])
- return split_string
- def camel_case_split(string):
- bldrs = [[string[0].upper()]]
- for c in string[1:]:
- if bldrs[-1][-1].islower() and c.isupper():
- bldrs.append([c])
- else:
- bldrs[-1].append(c)
- return [''.join(bldr) for bldr in bldrs]
- def camel_case_split2(string):
- # set the logic for creating a "break"
- def is_transition(c1, c2):
- return c1.islower() and c2.isupper()
- # start the builder list with the first character
- # enforce upper case
- bldr = [string[0].upper()]
- for c in string[1:]:
- # get the last character in the last element in the builder
- # note that strings can be addressed just like lists
- previous_character = bldr[-1][-1]
- if is_transition(previous_character, c):
- # start a new element in the list
- bldr.append(c)
- else:
- # append the character to the last string
- bldr[-1] += c
- return bldr
- RE_WORDS = re.compile(r'''
- # Find words in a string. Order matters!
- [A-Z]+(?=[A-Z][a-z]) | # All upper case before a capitalized word
- [A-Z]?[a-z]+ | # Capitalized words / all lower case
- [A-Z]+ | # All upper case
- d+ # Numbers
- ''', re.VERBOSE)
- assert RE_WORDS.findall('FOOBar') == ['FOO', 'Bar']
Add Comment
Please, Sign In to add comment