Applying Tag to Selected Text

Applying Tag to Selected Text

17
Basic UserBasic User
17

    Oct 09, 2014#1

    Hi,

    I want to apply tag for the selected text and I am using the below macro for that.

    Code: Select all

    Find "^s"
    Replace "<tag>^s</tag>"
    
    All I need is if the selected text contains [,\. :] in the end then the characters has to be skipped.

    For example,

    sample Text - <tag>sample Text</tag>
    sample Text. 123- <tag>sample Text. 123</tag>
    sample Text. - <tag>sample Text</tag>.
    sample, Text, - <tag>sample, Text</tag>,

    Please Advice

    6,603548
    Grand MasterGrand Master
    6,603548

      Oct 09, 2014#2

      Here is an UltraEdit script solution.

      Code: Select all

      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         if (UltraEdit.activeDocument.isSel())
         {
            UltraEdit.insertMode();
            UltraEdit.activeDocument.write(UltraEdit.activeDocument.selection.replace(/^([\s\S]+?)([,.:]?)$/,"<tag>$1</tag>$2"));
         }
      }
      I can try to find a macro solution as well, but the problem is ^ and $ mean here beginning and end of string and not beginning and end of a line as when using the same Perl regular expression replace from within a macro executed on selected text only.

        Oct 10, 2014#3

        Here is a macro solution. It is not an optimal solution and most likely there is a better method, but at least it works.

        Code: Select all

        IfSel
        UltraEditReOn
        Find MatchCase SelectText "^s"
        Replace "<tag>^s</tag>"
        Key LEFT ARROW
        Key Ctrl+LEFT ARROW
        Key LEFT ARROW
        Key LEFT ARROW
        Key LEFT ARROW
        IfCharIs ",.:"
        Find MatchCase RegExp "^(?^)^(</tag>^)"
        Replace "^2^1"
        Else
        Key RIGHT ARROW
        Key RIGHT ARROW
        Key RIGHT ARROW
        Key Ctrl+RIGHT ARROW
        Key RIGHT ARROW
        EndIf
        EndIf
        Best regards from an UC/UE/UES for Windows user from Austria

        17
        Basic UserBasic User
        17

          Oct 10, 2014#4

          Dear Mofi,

          Thanks for your reply.

          I have modified your script to the below macro and its working good so far. Does it produce any errors or misbehave in any cases?

          Kindly advice.

          Code: Select all

          PerlReOn
          Find RegExp SelectText "^([\., \r\n]+)([\s\S]+?)([,.: ]*?)$"
          Replace All "$1<other>$2</other>$3"
          Find RegExp SelectText "^([\s\S]+?)([,.: ]*?)$"
          Replace All "<other>$1</other>$2"
          Thanks in advance
          Arun

          6,603548
          Grand MasterGrand Master
          6,603548

            Oct 10, 2014#5

            It looks like you want a different behavior on multi-line selection as I thought you would like. I thought that with

            Code: Select all

            line 1
            line 2
            and both lines selected (without line termination on second line) should be changed to

            Code: Select all

            <tag>line 1
            line 2</tag>
            But your macro produces

            Code: Select all

            <tag>line 1</tag>
            <tag>line 2</tag>
            It is okay if that is the wanted behavior. I don't know all use cases for the macro. Therefore I cannot evaluate if there is a misbehave in any case.
            Best regards from an UC/UE/UES for Windows user from Austria

            17
            Basic UserBasic User
            17

              Oct 10, 2014#6

              Thanks for your reply.

              I am using this macro in a single line only. So

              Code: Select all

              <tag>line 1</tag>
              <tag>line 2</tag>
              
              was correct. And now I have a small problem in the macro.
              If I apply tag on "A. " the result was

              Code: Select all

              <other>A</other>. 
              this is correct.
              But If the text was ". A", the result was

              Code: Select all

              <other>. <other>A</other></other>
              but the result was supposed to be

              Code: Select all

              ". <other>A</other>"
              Please help me fix this issue.

              6,603548
              Grand MasterGrand Master
              6,603548

                Oct 10, 2014#7

                A simple solution for your problem is to run second replace only if first replace did not change anything.

                Code: Select all

                PerlReOn
                Find RegExp SelectText "^([\., \r\n]+)(.+?)([,.: ]*?)$"
                Replace All "$1<other>$2</other>$3"
                IfNotFound
                Find RegExp SelectText "^(.+?)([,.: ]*?)$"
                Replace All "<other>$1</other>$2"
                EndIf
                Or this code is used:

                Code: Select all

                PerlReOn
                Find RegExp SelectText "^([\., \r\n]+)(.+?)([,.: ]*?)$"
                Replace All "$1<other>$2</other>$3"
                Find RegExp SelectText "^((?:.(?!<other>))+?)([,.: ]*?)$"
                Replace All "<other>$1</other>$2"
                [\s\S] matching any whitespace character and any not whitespace character or in other words really any character is now replaced by a dot to match any character except newline characters (carriage return and line-feed).

                (?:.(?!<other>)) is a non-capturing group searching for any character except newline characters where NOT <other> follows. The expression (?!<other>) is a negative lookahead. In other words the second search expression ignores now lines containing already anywhere the tag <other>.
                Best regards from an UC/UE/UES for Windows user from Austria

                17
                Basic UserBasic User
                17

                  Oct 11, 2014#8

                  Thanks for your Reply.