- AuthorPosts
- May 27, 2023 at 9:01 am #29266spirosParticipant
I have a list of words (in a linked file) which I want to run to a text and have replaced any instances to wikilinks, i.e. find “text” and replace with “[[text]]”.
However, the existing text already contains words and phrases with wikilinks, they could be like these:[[test]] look at my [[test|testing]] how it tests I have a test, you know a [[test, and more test]] [[this is another test you know]]
Now, if the word is enclosed in brackets as listed above, then it should not be replaced. So, in the above example, the output should be for the “text” replacement:
[[test]] look at my [[test|testing]] how it tests I have a [[test]], you know a [[test, and more test]] [[there's another test you know]]
So, only one replacement in line 3.
The only way I thought of doing this is first to add an extra non-occurring character to words contained within double brackets. I.e first do something like
[[test한]] look at my [[test한|testing한]] how it tests I have a test, you know a [[test한, and한 more한 test한]] [[there한's한 another한 test한 you한 know한]]
And then run the replacements file. But I cannot figure out the regex to add the non-occurring character as described. Any thoughts on this approach, or any other indeed, would be appreciated.
May 28, 2023 at 9:02 am #29269Yutaka EmuraKeymasterYou can use:
(?<!\[)test(?!\])
(?!pattern)
Negative Lookahead
(?<!pattern)
Negative LookbehindMay 28, 2023 at 10:10 am #29270spirosParticipantThanks! Just tried with the test phrase but it also matches words within a phrase contained in double brackets, ie
[[a word, test, and more test]]
To keep it simple (and leave the find/replace linked file as is, ie. test→[[test]]) is it possible to add the extra character in any words between double brackets as mentioned, ie find:
I have a [[test]], you know a [[test, and more test]]
Replace
I have a test, you know a [[test한, and한 more한 test한]]
May 28, 2023 at 10:26 am #29271spirosParticipantCorrect replace should be:
I have a [[test한]], you know a [[test한, and한 more한 test한]]
May 28, 2023 at 10:45 am #29273spirosParticipantI think I got it:
Find: (?:\G(?!\A)|\[\[)(?:(?!\[\[|]]).)*?\K\w+
Replace: $0한 - AuthorPosts
- You must be logged in to reply to this topic.