truncate trailing digits from string without knowing number of trailing digits?

1159
7
Jump to solution
01-27-2014 07:27 AM
JohnDye
Occasional Contributor III
What's the most elegant way to truncate trailing digits from a string without knowing beforehand how many trailing digits you will encounter.
In particular, I want to be careful not to truncate digits contained within a given string's basename or those that a prefixed, only the suffix values that are digits.

Example:

"7-Eleven" : I don't want to truncate anything.
"7-Eleven 2" : I want to truncate the '2'
"7-Eleven 2364" : I want to truncate the '2364'.

That last part if a bit of an exaggeration, I have exceptions in place to keep things from getting crazy, but there could be instances where I don't know how many digits to expect beforehand and need to truncate them from the end of the string.

Thus far, I gotten to something along the lines of:
if str(MyLayer.name[-1]).isDigit():     newName = str(MyLayer.name[:-1]) elif str(MyLayer.name[-2:].isDigit():     newName = str(MyLayer.name[:-2])


You can see how this could go on and on...
I'm looking for some sort of dynamic method to determine the number of trailing characters I need to drop in order to remove all of the trailing digits from the Layer Name.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JoshuaChisholm
Occasional Contributor III
Hello John,

There are probably 1000 good ways to do this. One way is to use regular expressions, but since RegEx has complicated syntax (in my option), I would do something like this:
s='7-Eleven 2364' while s[-1:].isdigit() or s[-1:]==" ":     s=s[:-1]

Note it also gets rid of any tailing spaces.

Good luck!

View solution in original post

0 Kudos
7 Replies
JoshuaChisholm
Occasional Contributor III
Hello John,

There are probably 1000 good ways to do this. One way is to use regular expressions, but since RegEx has complicated syntax (in my option), I would do something like this:
s='7-Eleven 2364' while s[-1:].isdigit() or s[-1:]==" ":     s=s[:-1]

Note it also gets rid of any tailing spaces.

Good luck!
0 Kudos
JohnDye
Occasional Contributor III
Hello John,

There are probably 1000 good ways to do this. One way is to use regular expressions, but since RegEx has complicated syntax (in my option), I would do something like this:
s='7-Eleven 2364'
while s[-1:].isdigit() or s[-1:]==" ":
    s=s[:-1]

Note it also gets rid of any tailing spaces.

Good luck!


You know, it's so rare for me to encounter a condition where a while-loop is warranted that I didn't even think to use it. Suppose that's a testament to my infantile coding abilities, lol. Thanks Joshua!
0 Kudos
JasonScheirer
Occasional Contributor III
Just for reference, the regular expression you'd use and how you'd use it:

re.sub(" \d+$", "", s)


Where the " \d+$" means: one space, a digit(\d) one or more times(+), followed by the end of the string($).
0 Kudos
ClintDow
Occasional Contributor
You can do a one-liner like the following:

test = "7 Eleven 13435423"
test_trunc =  " ".join(test.split()[:-1]) if test[-1].isdigit() else test

Although regex looks cleaner, I personally haven't learned it yet either.
0 Kudos
JohnDye
Occasional Contributor III
You know, it's so rare for me to encounter a condition where a while-loop is warranted that I didn't even think to use it. Suppose that's a testament to my infantile coding abilities, lol. Thanks Joshua!


So little bit of a complication. I keep ending up in an infinite loop using the while-loop, but I'm not sure why.

Here's the code I'm using:
itemName = "Starbucks 1234_"
while str(itemName[-1:]).isdigit() or str(itemName[-1:]) == "_" or str(itemName[-1:]) == " ":
    itemDisplayName = itemName[:-1]


I just want to populate the result in 'itemDisplayName' and not neccesarily alter 'itemName' itself.
If I use:
itemName = "Starbucks 1234_"
while str(itemName[-1:]).isdigit() or str(itemName[-1:]) == "_" or str(itemName[-1:]) == " ":
    itemName = itemName[:-1]

Then everything goes smoothly. But it's when I try to populate the result in a different variable that it seems to run away into the abyss. Thoughts anyone??

UPDATE:
What I ended up doing in order to solve the above but continue using the while-loop but avoid the infinite loop was to just prepopulate 'itemDisplayName' with the contents of 'itemName' and THEN run the while-loop against the 'itemDisplayName' giving me the result in the variable I wanted.
itemName = "Starbucks 1234_"
itemDisplayName = itemName
while str(itemDisplayName[-1:]).isdigit() or str(itemDisplayName[-1:]) == "_" or str(itemDisplayName[-1:]) == " ":
    itemDisplayName = itemDisplayName[:-1]
0 Kudos
ClintDow
Occasional Contributor
In that case the loop is checking the last char in itemName repeatedly since you're only checking the last value of itemName while not removing it. Therefore the loops gets "_" over and over, however if you update itemName inside the loop it removes the last character one by one.

if you wanted to use that solution you would have to do something like:

i = 1
while not itemName[-i].isalpha():
    itemDisplayName = itemName[:-i]
    i+=1


Then itemDisplayName should work as expected.
0 Kudos
JohnDye
Occasional Contributor III
In that case the loop is checking the last char in itemName repeatedly since you're only checking the last value of itemName while not removing it. Therefore the loops gets "_" over and over, however if you update itemName inside the loop it removes the last character one by one.

if you wanted to use that solution you would have to do something like:

i = 1
while not itemName[-i].isalpha():
    itemDisplayName = itemName[:-i]
    i+=1


Then itemDisplayName should work as expected.


Oh that's nifty.
0 Kudos