pythonstringpython-resnakecasing

How to convert string to snakecase format in python


I made a function that can convert every string to snakecase but some of my string creating a problem. I used re module

ENTIRE CODE

import re

def toSnakeCase(string, restToLower : bool = False):
  string = re.sub(r'(?:(?<=[a-z])(?=[A-Z]))|[^a-zA-Z]', ' ', self.string).replace(' ', '_')

  if (restToLower == True):
    return ''.join(self.string.lower())
  else:
    return ''.join(self.string)

INPUT

strings = ['hello world', 'HelloWorld', '-HELLO-WORLD-', 'Hello-World', 'hello_world', '--hello.world', 'Hello-WORLD', 'helloWORLD']

# using enumerate just to see which list item creating problem
for i, j in enumerate(strings, 1):
  print(f'{i}. {toSnakeCaseV1(j)}')

OUTPUT - Without restToLower = True

1. hello_world
2. Hello_World
3. _HELLO_WORLD_
4. Hello_World
5. hello_world
6. __hello_world
7. Hello_WORLD
8. hello_WORLD

with restToLower = True

1. hello_world
2. hello_world
3. _hello_world_
4. hello_world
5. hello_world
6. __hello_world
7. hello_world
8. hello_world

As you can see item 3 and 6 creating the problem. Someone know why it is doing this according to me my regex is right.

Expected Output

1. hello_world
2. hello_world
3. hello_world
4. hello_world
5. hello_world
6. hello_world
7. hello_world
8. hello_world

Solution

  • Your problem seems just to be the leading and trailing _, remove them before of after the the space > _ conversion

    def toSnakeCase(string):
        string = re.sub(r'(?<=[a-z])(?=[A-Z])|[^a-zA-Z]', ' ', string).strip().replace(' ', '_')
        return ''.join(string.lower())
    

    For post-striping

    string = re.sub(r'(?<=[a-z])(?=[A-Z])|[^a-zA-Z]', ' ', string).replace(' ', '_').strip("_")