Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #29266
    spiros
    Participant

    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.

    #29269
    Yutaka Emura
    Keymaster

    You can use:

    
    (?<!\[)test(?!\])
    

    (?!pattern) Negative Lookahead
    (?<!pattern) Negative Lookbehind

    #29270
    spiros
    Participant

    Thanks! 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한]]

    #29271
    spiros
    Participant

    Correct replace should be:
    I have a [[test한]], you know a [[test한, and한 more한 test한]]

    #29273
    spiros
    Participant

    I think I got it:
    Find: (?:\G(?!\A)|\[\[)(?:(?!\[\[|]]).)*?\K\w+
    Replace: $0한

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.