Stop RegEx search after finding a specific character



  • Salutations, oh wise and venerable prophets of knowledge :)

    I have a .txt file that I have to open every day in N++ and must copy / paste data that occurs between brackets, i.e. [ stuff I have to copy must not include these brackets ]

    I have tried the following regex’s:
    (?<=[)[^}]+(?=]) — it gives me what I want – highlighted text in between brackets – but it goes to the very last ] in the entire document
    [([^]]+)([^]]+)] – it gives me what I want – it highlights only to the first ] it finds, but it includes the brackets [ ]

    What I would like, please, is a way that I can get both of those things in 1 expression, if possible

    The data I collect is in this format:
    ::Ihavechickenlegs::
    [I want to select the text
    on the following lines
    after the label listed above]

    ==================================
    ::Iampastylikedough::
    [This stinks to
    high heaven and I want
    a Big Mac]



  • @TTM-1895

    I assume you are looking for (?is)[(?>(?!<]).)*?]
    I can’t get the credits for this as it is just a modification from one of the great
    posts from @guy038, but I wasn’t able to find the original post, the ones with the
    <record> solution, otherwise I would have pointed to it as it also had a good explanation
    what exactly it is doing.

    Cheers
    Claudia



  • Thank you for the quick response :)

    When I plugged in the expression you gave me, it had the same effect as my expression #2… it still includes the brackets in the highlight. I would like it to highlight everything ‘in between’ the brackets, as it does in my expression #1, but stop at the first ] it finds.

    Any other ideas?

    Thank you for your time and efforts. :)



  • @TTM-1895

    I hope I finally understood your request, what about this (?is)(?<=[)(?>(?!<]).)*?(?=]) regex.

    By the way, what do you mean by it stops after it found the first ] ?
    Something you could use to replace the first block of data in a couple of files
    or just in terms of selecting one block of data after the other?

    Cheers
    Claudia



  • Eureka! That last expression you gave me was perfect!

    I guess I wasn’t explaining accurately enough, but you sussed it out and gave me precisely what I needed.

    Thank you tremendously for your expertise and assistance. :)

    Be well… and Cheers!



  • Hello, @ttm-1895, @claudia-frank and All,

    I think that the regex can be shortened, as below :

    (?s)(?<=[).+?(?=])

    Notes :

    • The (?s) modifier means that the dot regex character ( . ) represents any single char ( either a standard one or an EOL one )

    • The (?i) modifier is not necessary, as no letters or letter range(s) are involved in the discussed regexes

    • The main part of the regex is the .+? syntax, which catches the smallest NON-empty range of any character, which, either :

      • is preceded by a literal [ symbol, due to the (?<=[) positive look-behind structure

      • is followed with a literal ] symbol, due to the (?=]) positive look-ahead structure

    • Both [ and ] symbols must be escaped, with the \ symbol, to be interpreted as literals

    Cheers,

    guy038