| Forum Home | ||||
| Press F1 | ||||
| Thread ID: 119010 | 2011-06-30 22:14:00 | Python get last portion of string | Mike (15) | Press F1 |
| Post ID | Timestamp | Content | User | ||
| 1213668 | 2011-06-30 22:14:00 | Hi all, I have a python string something like: "Database Connection\\Database\\Table\\Record" or "Database Connection/Database/Table/Record" or "Database Connection/Database/Table" etc. I want to get the last portion of the string "Record", so want to get all characters after the last slash/backslash. There aren't always the same number of slashes, not always the same number of characters, and the database names and table names aren't always the same. Any ideas how I find the last instance of a character (the slash) and then take the remaining string after that character and return it to a new variable? Thanks, Mike. |
Mike (15) | ||
| 1213669 | 2011-06-30 22:42:00 | OK I think I've done it with a rough bit of regex... newstring = (re.compile('[\\\/]').split(string)[(len(re.compile('[\\\/]').split(string))-1):])[0] It appears to work anyway... I'm keen if anyone has a better/different/non-regex way to do it? :D Cheers, Mike. |
Mike (15) | ||
| 1213670 | 2011-07-01 03:04:00 | Use rfind or rindex with substring addressing - that's the most efficient way to achieve this, and doesn't involve the overhead of the regex parser. :pf1mobmini: |
Erayd (23) | ||
| 1213671 | 2011-07-05 20:45:00 | Use rfind or rindex with substring addressing - that's the most efficient way to achieve this, and doesn't involve the overhead of the regex parser.Thanks Erayd, that looks like it will do what I want :) cheers, Mike. |
Mike (15) | ||
| 1 | |||||