'Removing beginning of a string until the first instance of a specific character

I have a script that compiles an .exe file and runs the --tree=all of that .exe. This returns the directory where the file is stored and I want to use that path to run it.

This is the output I get from --tree=all

| | +-out\windows-x86-MD-mbcs-vs2008-rel\bin\VisualStudio08-32bit.exe

so I used the following to get rid of the special characters:

line = re.sub('[|+ -]', '', lines)

This works, but it removes all instances of - which results in this:

out\windowsx86MDmbcsvs2008rel\bin\VisualStudio0832bit.exe

But I want to get this:

out\windows-x86-MD-mbcs-vs2008-rel\bin\VisualStudio08-32bit.exe

How do I make sure only the first instance of - is removed, and the rest is left alone?



Solution 1:[1]

If you can rely on - preceding the name and you're not adamant on using a regex:

s = '| | +-out\windows-x86-MD-mbcs-vs2008-rel\bin\VisualStudio08-32bit.exe'
print s.split('-', 1)[1]

Output: out\windows-x86-MD-mbcs-vs2008-relin\VisualStudio08-32bit.exe

Solution 2:[2]

You will need to split the replace into two and for the second one limit the times it replaces to one.

line = re.sub('[|+ ]','',lines)
line = re.sub('-','',line, 1)

Solution 3:[3]

You could use re.sub('^[|+ -]*', '', line) to remove special characters at the start of the line.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mkrieger1
Solution 2 Kassym Dorsel
Solution 3