I need a quick and dirty regular expression.



  • This should be simple, but I’m messing it up somehow. All I want to do is search a large body of text and use a regular expression to replace all occurrences of “pen” or “crayon” with “pencil”, but I’m somehow failing to do this with what Notepad++ skills I have, can anyone help?

    For example, “I ate my pen and my crayons today” should become “I ate my pencil and my pencils today”.



  • Hello quickuuuu,

    No problem ! We’ll use the vertical bar character to search for the alternative patterns pen, and crayon, simultaneously.

    So :

    • Open the Replace dialog

    • Check the Regular expression search mode

    • Check the options Match case and Wrap around, if necessary

    • Type pen|crayon in the Fin what zone

    • Type pencil in the Replace with zone

    • If you previously select the block of text to operate, check also the In selection option

    • Finally, click on the Replace All button

    Et voilà !

    Best Regards

    guy038

    P.S. :

    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



  • Odd, I’ve tried exactly that to no success, is there anyway to debug my install? That could be an issue, I could even be out of date, is there anyway to check my version number?

    The only other thing that might be interfering is that my locale is Japanese, would that cause issues?



  • Hi quickuuuu,

    Just hit on the F1 key to get your present N++ version. Anyway, in order to use the PCRE, your version should be >= v6.0


    Secondly, you must NOT click twice on the Replace All button or you would replace, wrongly, the first part of the word pencil

    Therefore, a better syntax would be :

    SEARCH : \b(pen|crayon)(s)?\b

    REPLACE : pencil(?2s)

    Notes :

    • The \b form is an assertion which stands for the limit between a non-word character and a word character, or the opposite

    • The form (s)? is the final optional character s, located in group 2

    • In replacement, the syntax (?2s) means that we must rewrite the final s, if search group 2 exists

    Cheers

    guy038



  • Wow… I’m all the way back on 5.9.6.2, I’ll come back tommorow when I’ve got the new one. Are there any auto-update facilities?



  • Thanks, that’s done it!


Log in to reply