pythonregexsplit

Splitting based on particular pattern and editing string


I am trying to split a string based on a particular pattern in an effort to rejoin it later after adding a few characters.

Here's a sample of my string: "123\babc\b:123" which I need to convert to "123\babc\\"b\":123". I need to do it several times in a long string. I have tried variations of the following:

regex = r"(\\b[a-zA-Z]+)\\b:"
test_str = "123\\babc\\b:123"
x = re.split(regex, test_str)

but it doesn't split at the right positions for me to join. Is there another way of doing this/another way of splitting and joining?


Solution

  • You're right, you can do it with re.split as suggested. You can split by \b and then rebuild your output with a specific separator (and keep the \b when you want too).

    Here an example:

    # Import module
    import re
    
    string = "123\\babc\\b:123"
    
    # Split by "\n"
    list_sliced = re.split(r'\\b', "123\\babc\\b:123")
    print(list_sliced)
    # ['123', 'abc', ':123']
    
    # Define your custom separator
    custom_sep = '\\\\"b\\"'
    # Build your new output
    output = list_sliced[0]
    # Iterate over each word
    for i, word in enumerate(list_sliced[1:]):
        # Chose the separator according the parity (since we don't want to change the first "\b")
        sep = "\\\\b"
        if i % 2 ==  1:
            sep = custom_sep
        # Update output
        output += sep + word
    
    print(output)
    # 123\\babc\\"b\":123