How get string from first character in my example letter 'T' to first slash '/'
TEST/0001 need get TEST
TEST2/0001 need get TEST2
TEST3/0001 need get TEST3
How get string from first character in my example letter 'T' to first slash '/'
TEST/0001 need get TEST
TEST2/0001 need get TEST2
TEST3/0001 need get TEST3
In python, you can use split() function which returns array of elements splitted by the character you specify. Then you get the first element:
yourString = "TEST/0001"
yourString.split("/")[0]
>>> 'TEST'
I would go for the split
solution, but in case you are looking for a more complete and at the same time simple solution (assuming you know regexes, which anyway should belong to any programmer's knowledge) then you can use a few shortcut methods from the standard-library re module.
An example with your same data would be:
import re
lines = ["TEST/1000", "TEST2/1000", "TEST3/1000"]
pattern = "TEST\d*(?=/)" # Take any string beginning with TEST, followed by 0 or more digits and a / character
for line in lines:
match = re.match(pattern, line)
if match is not None:
print(match.group(0)) # match.group(0) returns the whole matched string, and not a part of it
else:
print("No match for %s" % line)
With my setup, running this script inside a test.py
file yields:
None@vacuum:~$ python3.6 ./test.py
TEST
TEST2
TEST3