Searching for string "\n" but not end of line?

Searching for string "\n" but not end of line?

8
NewbieNewbie
8

    Nov 12, 2013#1

    Hi,
    I have many files that contain \n as part of the text, like this:

    Code: Select all

    Post-trigger has started.\nSnapshot Setup popup will be closed.
    What I need to do is find the instances of \n and place tags around them. But I am having real problems differentiating between the written \n and the end of line.
    I've tried as regex and non-regex but neither seem to work.

    It's easy enough to do ordinarily, but when I put it in a script it all goes wrong!

    This is what I'm trying (there is obviously other text in the search too).

    Code: Select all

    UltraEdit.perlReOn();
    UltraEdit.frInFiles.regExp = true;
    UltraEdit.frInFiles.replace("(PGM-FI|jpg|png|\n)", "<ut Style='internal'>$1</ut>");
    I've tried escaping the \n and I've tried \n followed by not an end of line. Both of which sounds like good ideas but my implementation must be wrong because they don't work.

    Any suggestions?
    Thanks

    6,686585
    Grand MasterGrand Master
    6,686585

      Nov 12, 2013#2

      The Perl regular expression engine interprets \n as placeholder for a line-feed. So if you want to search for the string "\n", you have to escape the backslash with an additional backslash.

      So the Perl regular expression search string must be (PGM-FI|jpg|png|\\n)

      But that's not all. JavaScript engine interprets \n as placeholder for a line-feed and the escape character in a JavaScript string is also the backslash character like for the Perl regular expression engine. So if you would use

      UltraEdit.frInFiles.replace("(PGM-FI|jpg|png|\\n)", "<ut Style='internal'>$1</ut>");

      the JavaScript engine stores the search string in a string variable as (PGM-FI|jpg|png|\n) which is passed to the Perl regular expression which interprets now \n as line-feed character.

      Therefore the two backslashes in the Perl regular expression search string must be both escaped for the JavaScript string. The line to use is:

      UltraEdit.frInFiles.replace("(PGM-FI|jpg|png|\\\\n)", "<ut Style='internal'>$1</ut>");

      Now the JavaScript engine stores the search string as (PGM-FI|jpg|png|\\n) which is passed in this form to the Perl regular expression engine reading the string as (PGM-FI|jpg|png|\n) and the replace works as you would like it.
      Best regards from an UC/UE/UES for Windows user from Austria

      8
      NewbieNewbie
      8

        Nov 12, 2013#3

        That's very helpful.
        Thank you once again :)