Kunal Tanwar
Kunal Tanwar

Reputation: 1291

Given string is not converting into camelCase in python?

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.

Upvotes: 3

Views: 460

Answers (3)

trincot
trincot

Reputation: 350310

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']

Upvotes: 2

Mark Tolonen
Mark Tolonen

Reputation: 177745

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

Upvotes: 2

Cory Kramer
Cory Kramer

Reputation: 117886

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

Upvotes: 1

Related Questions