Removing first appearance of word from a string?
python, regex
Solution
Python's str.replace has a max replace argument. So, in your case, do this:
>>>mystring = "Description: Mary had a little lamb Description: "
>>>print mystring.replace("Description: ","",1)
"Mary had a little lamb Description: "
Using regex is basically exactly the same. First, get your regex:
"Description: "
Since Python is pretty nice about regexes, it's just the string you want to remove in this case. With that, you want to use it in re.sub, which also has a count variable:
>>>import re
>>>re.sub("Description: ","",mystring,count=1)
'Mary had a little lamb Description: '
Problem
I'm not familiar with regex, and it would be great if someone giving a solution using regex could explain their syntax so I can apply it to future situations. I have a string (ie. `'Description: Mary had a little lamb'`), and I would like to remove `'Description: '` such that the string would read `'Mary had a little lamb,'` but only the first instance, such that if the string was `'Description: Description'`, the new string would be `'Description.'` Any ideas? Thanks!