URL: https://www.peterbe.com/plog/jacobian-highlighter/highlight.html

My friend Jacob from Galdrion taught me about "positive lookbehind assertion" and "lookahead assertion" when writing regular expressions in Python. It was new to me and I can't believe why I didn't read up on this more earlier because they're really useful. I've now got a usage for these which I use to find words that written on their own. For example, in the string "peterbe" the word "peter" doesn't exist really. You only want to find your words when they're written alone. You can't rely on it being spaces always on both sides either. You might find brackets, fullstops, end-of-string, you name it.

Enough chatting about it, I have now put together a little something that does this properly which I for no real reason call:

the Jacobian highlighter

Please give it a spin to see if it behaves like you expect it too. If you find any problems, let me know and I'll fix it and add them to the unit tests.

Next on the todo list for this is Unicode support of course.

Comments

Anonymous

You might be interested in the \b pattern which matches the break at the beginning or end of a word.

Peter Bengtsson

Forgot about that one and will try to use it instead of the currently complicated one.

Your email will never ever be published.

Related posts