Community
    • Login

    Find text and replace at the top of file

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    10 Posts 3 Posters 527 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Srirup DasS
      Srirup Das
      last edited by

      Hi,
      Is there a way to find text, in this case <!DOCTYPE HTML> in all JSP files and replace it at the top of the file. Like, cut and paste at the top. If this is not possible, I would like to find all files which doesn’t have <!DOCTYPE HTML> at the beginning.

      Alan KilbornA 1 Reply Last reply Reply Quote 0
      • Alan KilbornA
        Alan Kilborn @Srirup Das
        last edited by Alan Kilborn

        @Srirup-Das

        A technique for matching your data only at top-of-file would be searching for this regular expression: (?<!\x0A)^<!DOCTYPE HTML>

        I had bookmarked the origin of this technique, and it is here if you want to read more.

        Srirup DasS 1 Reply Last reply Reply Quote 3
        • Srirup DasS
          Srirup Das @Alan Kilborn
          last edited by

          @Alan-Kilborn Thanks for your response. What would be the regular expression to find in files which doesn’t have <!DOCTYPE HTML> at the top of the file.

          Alan KilbornA 1 Reply Last reply Reply Quote 0
          • Alan KilbornA
            Alan Kilborn @Srirup Das
            last edited by Alan Kilborn

            @Srirup-Das

            For that, I’d be tempted to try (?<!\x0A)^(?!<!DOCTYPE HTML>).

            Note that this one matches only a single character when the special string is NOT in a file.

            But my eyes are swimming…the usage of this sequence ?<! versus this one ?!<! meaning two totally different and unrelated things. :-)

            Srirup DasS 1 Reply Last reply Reply Quote 3
            • Srirup DasS
              Srirup Das @Alan Kilborn
              last edited by

              @Alan-Kilborn Seems like (?<!\x0A)^(?!<!DOCTYPE HTML>) is working. Thanks for your help.

              Srirup DasS 1 Reply Last reply Reply Quote 1
              • Srirup DasS
                Srirup Das @Srirup Das
                last edited by

                @Alan-Kilborn One more thing I found is (?<!\x0A)^(?!<!DOCTYPE HTML>) is listing down the files which doesn’t have <!DOCTYPE HTML> at all in the file. I don’t need those files. I need only the files which contains <!DOCTYPE HTML> but not at the top.

                Alan KilbornA 1 Reply Last reply Reply Quote 0
                • Alan KilbornA
                  Alan Kilborn @Srirup Das
                  last edited by

                  @Srirup-Das

                  Okay, okay, one more freebie, but after that unless I start charging your for consulting, the others here (and really, me, too) are gonna get all over me/us. :-)

                  (?<!\x0A)^(?!<!DOCTYPE HTML>)(?s).*?<!DOCTYPE HTML>

                  NOW, What you owe ME is to interpret this and reply back with how it works (assuming it does)!

                  1 Reply Last reply Reply Quote 3
                  • guy038G
                    guy038
                    last edited by

                    Hello, @srirup-das, @alan-kilborn and All,

                    I do not get much credit for intervening at the end of the discussion to announce that the negative look-ahead (?!<!DOCTYPE HTML>) sems useless, in the last version of the regex !

                    Indeed, this shorter syntax (?<!\x0A)^(?s).+<!DOCTYPE HTML> is enough to get the job done ;-)) As the former regex, it even detects files whose the first line is, let’s say, as ABC<!DOCTYPE HTML> ( so, not exactly at the very beginning of current file ) !

                    Best Regards,

                    guy038

                    Alan KilbornA 1 Reply Last reply Reply Quote 3
                    • Alan KilbornA
                      Alan Kilborn @guy038
                      last edited by

                      @guy038

                      True enough, although these discussions tend to evolve to “build up a regex” by just adding on to what was said last. That’s what I did in this case.

                      Throwing most of it out might have been more confusing to the OP.

                      But I’d still like the OP to answer my question as well as now a second question: Why the + in @guy038 's regex is the absolute key to the whole thing!

                      Srirup DasS 1 Reply Last reply Reply Quote 1
                      • Srirup DasS
                        Srirup Das @Alan Kilborn
                        last edited by

                        Thanks @Alan-Kilborn and @guy038 . Both of them worked what I was looking for. I’m still new to regular expressions, but I will try to dig in more on both of your solutions.

                        1 Reply Last reply Reply Quote 2
                        • First post
                          Last post
                        The Community of users of the Notepad++ text editor.
                        Powered by NodeBB | Contributors