Search and Replace



  • Hello, this is my first post on this forum.

    I am in no way a scripter, but I was doing some modding on a game, and I couldn’t figure out how to use the “Find in Files” function to do what I wanted.
    I want to replace part of a text in several files in a directory, but where I want to make some parts irrelevant for the search, if you understand.

    officers = {
    	culture = pashtun #This doesnt matter, they vary.
    	religion = sunni #This doesnt matter either, they vary.
    	size = 625 #I want to change this.
    }
    

    I want to change the size, but the culture “Pashtun” and the religion “Sunni” doesn’t matter, because there are many different cultures and religions in this file directory, for example English, French, Protestant, and Hindu.

    I hope someone understands my problem.
    Thanks in advance =)



  • @Max-Astrup
    If you want a specific answer, I think you’ll have to provide a specific example. Also, examples of what you tried that did not work might be helpful.

    Here is a link that describes regular expressions which will likely be what you need to carry out your task: http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html



  • Hello Max,

    So, from you example, below, I suppose that you would like to change the value 625 of the tag size, for an other one, whatever the contents of the two previous lines are ?

    officers = {
        culture = pashtun #This doesnt matter, they vary.
        religion = sunni #This doesnt matter either, they vary.
        size = 625 #I want to change this.
    }
    

    Well, I think that a good search/replacement, using regular expressions, could be :

    SEARCH (?s-i)^officers.+?size = \K\d+

    REPLACE <some digits> ( The new value of the tag size )

    So :

    • Open your file, in Notepad++

    • Open the Replace dialog ( Ctrl + H )

    • Fill the Find what field with the above regex

    • Fill the Replace with field with a new integer value, for the tag size

    • Uncheck, preferably, the Wrap around option

    • Click, EXCLUSIVELY, on the Replace All button ( DON’T use the Replace button, for a step by step replacement. Indeed, nothing is changed, in that case ! )

    Et voilà !


    If you’re a newby, about regular expressions concept and syntax, begin with that article, in N++ Wiki :

    http://docs.notepad-plus-plus.org/index.php/Regular_Expressions

    In addition, you’ll find good documentation, about the new Boost C++ Regex library, v1.55.0 ( similar to the PERL Regular Common Expressions, v1.48.0 ), used by Notepad++, since its 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 explains the syntax, of regular expressions, in the SEARCH part

    • The SECOND link explains the syntax, of regular expressions, in the REPLACEMENT part


    You may, also, look for valuable informations, on the sites, below :

    http://www.regular-expressions.info

    http://www.rexegg.com

    http://perldoc.perl.org/perlre.html


    If you’re, already, fairly acquainted with regexes, here are, below, some notes about this specific regex :

    • The first part (?s-i), of the **search regex, are two modifiers :

      • The first one, (?s), forces the dot special character to represent any single character ( a standard character or an EOL character )

      • The second one, (?-i), forces the regex engine to do this search/replacement, in a sensitive case way

    • The part ^officiers, looks, for beginning of the line, ^, for the string officiers, in that exact case

    • Then, the part .+?size =, tries to match the shortest, non-null, range of any character, till the string size =, with a space character, after the equal sign ( Why the shortest ? Because it must be located inside the officiers {......} block ! )

    • Now, the \K form, forces the regex engine to forget everything that was previously matched and resets the caret location to the position between the space character, after the equal sign, and the first digit of the size value

    • Finally, the part \d+ matches all the digits of the size’s value

    • In replacement, just type the new value of the size tag, which will replace the old value


    But, I guess that you showed your example, split, in several lines, in order to tell us, about the right zone to be changed ! So your text would rather be written, as below :

    officers = { culture = pashtun religion = sunni size = 625 }
    

    In that case, the search regex should be, simply re-written, as below :

    (?-si)^officers.+size = \K\d+

    • This time, the part .+ represents the longest range of standard characters only, of the current line ( due to the (?-s) modifier ), till the string size =, with a space character after the equal sign

    Best Regards,

    guy038


Log in to reply