darrenvba
darrenvba

Reputation: 221

Delete all characters after a backslash in python?

I have a for loop that changes the string current_part. Current_part should have a format of 1234 but sometimes it has the format of 1234/gg

Other formats exist but in all of them, anything after the backlash need to be deleted.

I found a similar example below so I tried it but it didn't work. How can I fix this? Thanks

current_part = re.sub(r"\B\\\w+", "", str(current_part))

Upvotes: 0

Views: 1400

Answers (3)

JMat
JMat

Reputation: 737

You can split your string using string.split()

for example:

new_string = current_part.split("/")[0]

Upvotes: 1

XZ6H
XZ6H

Reputation: 1789

Find the position of '/' and replace your string with all characters preceding '/'

st = "12345/gg"
n = st.find('/');
st = st[:n]
print(st)

Upvotes: 2

Efferalgan
Efferalgan

Reputation: 1701

No need for regexes here, why don't you simply go for current_part = current_part.split('/')[0] ?

Upvotes: 4

Related Questions