Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.


Wildcard search and replace help

2. Help
  • Simon Harris
    Simon Harris


    Can anyone help with a search and replace question

    I am trying to find any text within apostrophe ie 'MM5 BELL'
    and replace the apostrophe with brackets ie [MM5 BELL]

    Note! The text within the apostrophe varies in content so im looking for a global change of any text/numbers that are within apostrophes and change the apostrophes for []

    I have searched a few topics but couldnt find this type

    Last edit: Simon Harris 2014-04-01
  • Loreia2

    Hi Simon,

    try this.

    Ctrl + H to bring replace dialog

    Find what: '(.*?)'
    Replace with: [\1]

    Tick Regular expression, and hit Replace all.


  • GerdB

    Use search/replace with Regular Expressions activated:

    Search: '([^']*)'
    Replace: [\1]

    Explanation: Find an apostrophe followed by any characters which are not apostrophes followed by another apostrophe and mark the text between the apostrophes, then replace the whole thing with an opening bracket followed by the previously marked text followed by a closing bracket.


    Hello GerdB and All,

    GerdB, I don't understand why you preferred your search regex to the Loreia2's one ?

    Indeed, most of the time, the two regexes '.*?' and '[^']*' match the same strings.

    But, practically, they are slightly different !

    Don't forget that :

    • In Notepad++, the dot . represents a single character, different that \n, \r and \f => . = [^\n\f\r]

    • In most regex engines, the form [^'] means any character different from the single quote (')), included the EOL characters Therefore, [^'] may represent the \n or the \r character, at the end of a line !

    So, if we consider the example text, below, with the string 'Test3', split in two lines :


    • Your regex '[^']*' matches the strings 'Test1', 'Test2' and 'Test4' but also the word 'Te\r\nst3', divided in two consecutive lines !

    • And the Loreia2 regex '.*?' matches ONLY the two strings 'Test1', 'Test2' and, of course, the string 'ABCDEFGH' !

    It's also important to point out the difference between the .* form and the .*? form :

    • The regex '.*?' matches two single quotes, in the same line, with the shortest string, even empty, between these two single quotes. So, between the two quotes, it can't contain any other single quote !

    • The regex '.*' matches two single quotes, in the same line, with the longest string, even empty, between these two single quotes

    So, the regex '.*' would match the two strings 'Test1'12345'Test2'67890' and 'ABCDEFGH'Test4' in our example text above !

    => Notice that, with that form, these two occurrences do contain single quote(s) themselves !!



    P.S. :

    To obtain the same regex of Loreia2, you need to use the regex '[^'\r\n]*'

    Last edit: THEVENOT Guy 2014-04-06