delete first X words and delimiters of a string - with multiple delimiters

Viewed 246

I have a string such as manipulate widgets add,1,2,3 (sorry, but I can't change the format).

I want to delete the first X words and any delimiters which preced them.

Let's take 3 as an example, thus deleting manipulate widgets add and leaving ,1,2,3

Or, take manipulate,widgets,add,1,2,3 delete two words (manipulate,widgets) and leave ,add,1,2,3

I can split the string into a list with words = re.split('[' + delimiters + ']',inputString.strip()) but I can't simply delete the fist X words

with, say,

for i in range(1, numWorsdToRemove):
            del words[0]

and then return ' '.join(words) because that gives me 1 2 3 4.

How can I do it and retain the original delimiters of the non-deleted words?

Just to make it interesting, the input string can contain multiple spaces or tabs between words; only one comma, but that might also have spaces pre/suc-ceeding it :

manipulate ,widgets add , 1, 2 , 3

Note that words are not guaranteed to be unique, so I can't take the index of the word after those to be deleted and use it to return a positional substring.


[Update] I accepted 'Kasramvd solution, but then found that it didn't correctly handle remover('LET FOUR = 2 + 2', 2) or remover('A -1 B text.txt', 2), so now I am offering abounty.


[Update++] delimiters are space, tab and comma. Everything else (including equals sign, minus sign, etc) is part of a word (although I would be happy if answerers would tell me how to add a new delimiter in future, should it become necessary)

8 Answers
Related