I spent most of the evening on this so I thought I'd post it anyway, despite Leith Ross having provided a solution some hours ago ;-)
My biggest problem was the Regular Expression coding ... hence I have plagiarised this from Leith. Thanks.
I'd also worked out a different test for the multiple underscores and I've replaced that with Leith's second RegExp test.
The code is a little different, reflecting styles more than anything. One key point is that I tag the log entries onto the end which you may or may not want.
I hope you will find the alternative approach gives you some ideas.
Regards
Bookmarks