Help with regex

  • Davey

    Davey - 2013-10-30

    I am trying to find text using regex, I can't see whay it's not working
    My string looks like this



    I am trying to find only "Global" in this circumstance and delete it
    They always appear at the beginning of a line

    I tried this:
    and it worked... however, since I want to delete the word Global (with its line feed), that regex won't be good enough for me.

    So I tried with a back reference like this
    but NPP tells me invalid regular expression!
    I've tried it in other applications and it did work

    If someone can help me, I would really appreciate it


  • Loreia2

    Loreia2 - 2013-10-31

    Hi Davey,

    why don't you just parse out Global keyword?


  • Davey

    Davey - 2013-10-31

    Hi Loriea - Thanks for your reply
    The truth is that thats is what I did in the end
    I dont know why I didnt do that in the first place but...
    Anyway, I still wanted, and would like, to know what is wrong with the regex expression that I wrote in my first post - Just for learning and understanding purposes


  • Loreia2

    Loreia2 - 2013-10-31

    Hi Davey,

    I am not sure what did you tried to achieve with second RE, but construct like (? doesn't make sense because ( starts a group, you need to escape it if you are searching for brace \(, and ? is repetition of previous character zero or one times.
    So, you didn't specify which character is being repeated.
    And I don't understand why you need <= construct, these characters are being searched for directly, they are not substitute for anything.


  • jmwelch

    jmwelch - 2013-10-31

    @Loreia2 - He was trying to use what this RegEx Tutorial calls a 'Lookbehind'. Essentially, within the parentheses, he's specifying a condition that must be met, but will not be returned with the captured group. Here's an example on RegExr, which uses a lookbehind to capture the word 'the', but only if it is preceded by 'to'.

    @Davey - To answer your question, the reason that (?<=^\w+\n)Global\n does not work in N++ is that N++ uses PCRE, which requires that lookbehind groups be of fixed length (which \w+ is not).

    To make a search/replace similar to your desired one, but without using the lookbehind, try this:

    Find what: (^\w+)\RGlobal\R
    Replace with: \1\R

    This also has the benefit of removing the newline from the leading edge of 'Global', just in case one happens to occur at EOF with no newline.

  • Davey

    Davey - 2013-10-31

    Hi jmwelch
    Thanks a lot for the explanation and clarification!!
    This is an important detail to understand and remember
    Your search method worked perfect!

    All the best


    THEVENOT Guy - 2013-11-02

    Hello, Jmwelch, Davey, Loreia2 and All

    Jmwelch, your explanation about fixed length groups in lookbehinds is quite correct.

    On the contrary, lookaheads don't have this limitation. For example, the search, in regular expression mode, of : abc(?=foo.*bar) select any string abc if it's immediately followed by the string foo and somewhere, further, on the same line, by the string bar.

    Concerning the Search/Replacement, that you gave to Davey, I think there's still a problem because the form \R, which means any form of EOL ( \r\n or \n or \r ), in the search part, is NOT allowed in the replacement part and should be replaced with \r\n or \n or \r, depending on the file type of EOL

    Now, if we include this EOL in the group 1, we avoid writing it and even shorten globally the regex !

    SEARCH : ^(\w+\R)Global\R?

    REPLACE : \1

    I just added a question mark, after the last \R, just in case a last word Global would end the file, without any EOL located afterwards !



    Last edit: THEVENOT Guy 2013-11-02
  • Davey

    Davey - 2013-11-08

    Hi Guy,
    Once again with an awesome clear explanation
    This forum is really great
    A big thanks to everyone!
    Especially to:
    Don (for the program),
    Loreia2 (once again for UDL2!) and
    cChris (for putting a lot of effort into the forum and plugins



Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks