sergio_101 · 2022-06-06 21:54
I am writing a script that looks at an org-mode buffer and replaces strings with an org-mode hyperlink.
For instance, I want to replace every instance of "Link One" with `[[LinkUrl][Link One]]`.
The problem is if I run my function again, that instance will now look like `[[LinkUrl][[[LinkUrl][Link One]]]]`.
I need to replace the text only if it is not surrounded by brackets.
MitchellMarquez42 · 2022-06-06 22:05
Include the spaces in both.
artyhedgehog · 2022-06-06 22:22
I would try the following:
- `\bLink One\b`
- `\([^[]\)Link One\([^]]\)` -> `\1[[link-url][Link One]]\2`
There is probably a better way to specify "sequence before" and "sequence after" without replacing them, but I'm not sure if that's available in emacs regexp.
FrozenOnPluto · 2022-06-07 00:33
If you’re not doing hundreds … query-replace can be handy in a pinch
sergio_101 · 2022-06-07 01:53
There might be a TON of these, as the list will be growing every week.
sergio_101 · 2022-06-07 01:54
okay, I think this is a good approach.. like, only match what SHOULD be there.. like "$%\^." or white space..
7890yuiop · 2022-06-07 03:02
`\bLink One\b` is likely to match for `[Link One]`, but the second approach certainly works.
Elisp regexps do not support arbitrary zero-width look-ahead/behind assertions, but this is generally fine because elisp allows you operate on specific groups of the matched text -- typically by calling `replace-match` and specifying a SUBEXP argument (group number).
In general you do the broader matching in your main `(while (re-search-forward...` loop and then you get to process each match however you want to (which could involve further regexp matching/replacement on that section of text).