Reputation: 8722
Lets say I have a bunch of strings, and they can only be in the following formats:
format1 = 'substring1#substring2'
format2 = 'substring1$substring2'
format3 = 'substring1'
Let me explain. The strings are sometimes divided using the #
or $
character. However other times, they are not.
I want to remove the part that appears after the #
or $
, if it exists. If it was just one special character, that is #
, I could have done this:
string = string.split('#')[0]
But how can I do it for the 2 special characters in a quick and elegant way? Also assume the following things:
Thanks.
Upvotes: 0
Views: 91
Reputation: 300
I think you can use a list to maintain the special characters that can be used and for every of them check if is present in the string, when you find one execute the splitting process and retrieve only the left part like so:
delimters = ["#","$"]
for symbol in delimters:
if symbol in string1:
left_part = string1.split(symbol)[0]
Now this approach has some disadvantages but is the simplest in my opinion. The problem is that if you have more than one string you need to nested loops.
Upvotes: 0
Reputation: 164673
You can use a for
loop to split by an arbitrary number of delimiters. Regular expression is typically less efficient than Python str
methods.
def converter(x, delims='#$'):
for delim in delims:
x = x.split(delim, maxsplit=1)[0]
return x
format1 = 'substring1#substring2'
format2 = 'substring1$substring2'
format3 = 'substring1'
for value in [format1, format2, format3]:
print(converter(value))
# substring1
# substring1
# substring1
Upvotes: 0
Reputation: 61910
Use replace before split:
format1 = 'substring1#substring2'
format2 = 'substring1$substring2'
format3 = 'substring1'
print(format1.replace('#', '$').split('$')[0])
print(format2.replace('#', '$').split('$')[0])
print(format3.replace('#', '$').split('$')[0])
Output
substring1
substring1
substring1
Upvotes: 0
Reputation: 1338
Use re.split() for it.
import re
print(re.split("#|$","STRING#OTHER_STRING#OTHER_STRING_2"))
Upvotes: 1
Reputation: 7840
If you want to avoid regex, one possibility would be:
string = min(string.split('#')[0], string.split('$')[0])
Upvotes: 2
Reputation: 61289
Regular expressions.
import re
re.sub('[$#].*', '', string_to_modify)
Upvotes: 2