3

Recently I have a made a function in python which converts any string in camelCase format, but while passing different types of string in this function I got into a problem when I tried to pass "BirdFlight" it did not convert it into camelCase.

NOTE - I used python built-in re module.

import re

def camelCase(string):
    string = re.sub(r'(_|-)+', ' ', string).title().replace(' ', '')
    
    return ''.join([string[0].lower() + string[1:]])

After that I created a list of 3 different types of string to pass in the function.

differentTypeOfString = ['bird flight', 'BirdFlight', '-BIRD-FLIGHT-']

for i in differentTypeOfString:
    print(camelCase(i))

ignore the varialbe name I will change it after.

OUTPUT

#1 birdFlight
#2 birdflight
#3 birdFlight

Well there are more problems I encountered just now . I increased the string types inside my list:

differentTypeOfString = ['bird flight', 'BirdFlight', '-BIRD-FLIGHT-', 'Bird-Flight', 'bird_flight', '--bird.flight', 'Bird-FLIGHT', 'birdFLIGHT']

Now I am getting this Output

#1 birdFlight
#2 birdflight
#3 birdFlight
#4 birdFlight
#5 birdFlight
#6 bird.Flight
#7 birdFlight
#8 birdflight

Same problem with "birdFLIGHT"and in "--bird.flight" the "." (dot) is not removing.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Kunal Tanwar
  • 1,209
  • 1
  • 8
  • 23

3 Answers3

2

This works for your current examples. The re.sub adds a space between any existing <lower><upper> sequence.

import re

def camelCase(string):
    string = re.sub(r'(?:(?<=[a-z])(?=[A-Z]))|[^a-zA-Z]', ' ', string).title().replace(' ', '')
    return ''.join([string[0].lower() + string[1:]])

differentTypeOfString = ['bird flight', 'BirdFlight', '-BIRD-FLIGHT-', 'Bird-Flight', 'bird_flight', '--bird.flight', 'Bird-FLIGHT', 'birdFLIGHT']

for i in differentTypeOfString:
    print(camelCase(i))

Output:

birdFlight
birdFlight
birdFlight
birdFlight
birdFlight
birdFlight
birdFlight
birdFlight
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
2

I would also suggest re.findall (or even re.finditer), and make the regex such that a word break is guaranteed between a lowercase and uppercase letter:

def camelCase(string):
    return "".join(word.title() if i else word.lower()
        for i, word in enumerate(re.finditer(r"[A-Z]*[a-z]+|[A-Z]+", string))
    )

For the following testcases the output is birdFlight:

['bird flight', 'BirdFlight', '-BIRD-FLIGHT-', 'Bird-Flight', 
 'bird_flight', '--bird.flight', 'Bird-FLIGHT', 'birdFLIGHT']
trincot
  • 317,000
  • 35
  • 244
  • 286
1

You could use findall with just looking for '\w+', then .title every word. Then just lowercase the first character

def camelCase(s):
    titled = ''.join(i.title() for i in re.findall('\w+', i))
    return titled[0].lower() + titled[1:]

Then your example

differentTypeOfString = ['bird flight', 'BirdFlight', '-BIRD-FLIGHT-']
for i in differentTypeOfString:
    print(camelCase(i))

output

birdFlight
birdflight
birdFlight
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • Now I increased my ```list``` and it is not working with the recently added elements or string types. – Kunal Tanwar Jul 29 '21 at 17:15
  • @KunalTanwar For `'birdFLIGHT'` how are you supposed to know it should be `'birdFlight'` and not `'birdflight'`? Is it because there is a lowercase to uppercase change between `d` and `F`? – Cory Kramer Jul 29 '21 at 17:17
  • That is all the game about!! I know that but I want python to this about itself. – Kunal Tanwar Jul 29 '21 at 17:19