Find/Replace part of each string using Regular Expressions

2. Help
Steiner
2013-11-11
2013-11-11
  • Steiner
    Steiner
    2013-11-11

    Hello all, I apologise for something which must have been asked about many times by the less knowledgeable, such as myself. Still there are many variations on this topic and I couldn't find a solution on my own, even after consultig with existing threads.

    What I need is a solution for replacing a part of string using wildcards.

    In my file I have something that looks like:

    id = 91516,
    ...
    id = 91738,
    ...
    id = 91...,

    and it all needs to become:

    id = 5516,
    ...
    id = 5738,
    ...
    id = 5...,

    where the last three characters before comma are a wildcard, there is always a comma. Now I have no problem finding the matching strings, but have trouble with replacement as the Replace function in Notepad++ doesn't preserve "..." characters before comma.

    This is what I am trying to input:

    Find: (no problem)
    id = 91...,

    Replace: (trouble)
    id = 5...,

    The trick is, the number of digits after "id = " should always be 5, as I don't want to capture any of these: id = 9118, id = 916 or id = 910035, they all do exist alongside and the file is very large with thousands of id's used throughout.

    Help would be much appreciated, many thanks in advance.

     
    Last edit: Steiner 2013-11-11
  • THEVENOT Guy
    THEVENOT Guy
    2013-11-11

    Hello Steiner,

    I think that I found out the right regular expression to achieve your needs :-)

    So, in Regular expression search mode :

    SEARCH : 91(\d\d\d,) with ONE space BEFORE the number 91

    REPLACE : 5\1 with ONE space BEFORE the digit 5

    Note :

    As the five digits number 91\d\d\d is enclosed with a space before and a comma after it can't match lines of your file, like "id = 913" or "id = 9123456" and so on...


    You can, instead, use a lookahead, in the search expression :

    SEARCH : 91(?=\d{3},) with ONE space BEFORE the number 91

    REPLACE : 5 with ONE space BEFORE the digit 5

    Note :

    After matching the string " 91", the lookahead, which is NOT part of the matched characters, looks, if at current position, it exists three digits, followed by a comma. If so, the regex matches and replace the string " 91" by the string " 5" ( with space before the two numbers 91 and 5 )


    And, if there's ONLY digits, after the string = 91, in your file, you can even shorten this regex by replacing the two expressions \d\d\d and \d{3} with only ... ( 3 dots)

    Hope this will be useful to you !!!

    Best regards,

    Guy038

    P.S. :

    You can find good documentation, about the new PRCE Regular Expressions, used by N++, since the 6.0 version, at the TWO addresses below :

    http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html

    http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/format/boost_format_syntax.html

    The FIRST link concerns the syntax of regular expressions in SEARCH

    The SECOND link concerns the syntax of regular expressions in REPLACEMENT

     
    Last edit: THEVENOT Guy 2013-11-11
  • Steiner
    Steiner
    2013-11-11

    Worked perfectly, thank you so much, Guy038!!!