Python regex – substitute all occurrences of keyword not preceded by specific characters

I'm trying to replace all occurrences of keyword not preceded by # or [[ with [[keyword]]. For instance, the strings

keyword
#keyword
some keyword
[[keyword]] keyword

would be replaced with

[[keyword]]
#keyword
some [[keyword]]
[[keyword]] [[keyword]]

I've tried ^(?!#.*$)keyword which does not work when keyword has at least one other word in front of it. Removing the ^ however causes all occurrences of keyword to be replaced, including the ones directly after a pound sign and brackets. How can I replace all occurrences of keyword except when following a # or [[?



Read more here: https://stackoverflow.com/questions/68466272/python-regex-substitute-all-occurrences-of-keyword-not-preceded-by-specific-ch

Content Attribution

This content was originally published by Simon Carlson at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: