• Login
Community
  • Login

Regex: How do I search for My_String [any characters] [quotation mark] ?

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
18 Posts 4 Posters 3.7k 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.
  • P
    PeterJones @IanSunlun
    last edited by PeterJones Nov 15, 2022, 7:58 PM Nov 15, 2022, 7:56 PM

    @IanSunlun ,

    Because you did not use the formatting toolbar when entering your post, and didn’t follow the advice given in the FAQ section about how to ask search/replace questions and format those so that all your characters come through, your data does not come through correctly.

    But based on your problem statement, FIND = My_String(.*)" , REPLACE = My_string${1}.mhtml" should work, as long as you have SEARCH MODE = Regular Expression selected.

    edit: Actually, I would recommend My_String(.*?)" to avoid matching too far. And (?-s)My_String(.*?)" so that I don’t have to tell you to not have . matches newline checkmarked.

    ----

    Useful References

    • Please Read Before Posting
    • Template for Search/Replace Questions
    • Formatting Forum Posts
    • FAQ: Where to find regular expressions (regex) documentation
    • Notepad++ Online User Manual: Searching/Regex
    I 1 Reply Last reply Nov 15, 2022, 8:05 PM Reply Quote 2
    • I
      IanSunlun @PeterJones
      last edited by IanSunlun Nov 15, 2022, 8:05 PM Nov 15, 2022, 8:05 PM

      @PeterJones Ah, thanks… sorry about the formatting…
      That appears to work. I looked up *? “Zero or one of the last character.” I’m not sure what that means exactly in my context… Just the .* worked for me fine.
      I don’t have the .newline checked.

      P 1 Reply Last reply Nov 15, 2022, 8:13 PM Reply Quote 0
      • P
        PeterJones @IanSunlun
        last edited by Nov 15, 2022, 8:13 PM

        @IanSunlun said in Regex: How do I search for My_String [any characters] [quotation mark] ?:

        @PeterJones Ah, thanks… sorry about the formatting…
        I’m not sure what that means exactly in my context… Just the .* worked for me fine.

        That means you didn’t have any lines with My_String/Blah/Blah" and "more text here", because otherwise it would have grabbed too much in

        That appears to work. I looked up *? “Zero or one of the last character.”

        Actually, alone, ? means “zero or one of the last character”, so if you had a? it would match zero or one a, and if you had [a-z]? it would match zero or one letter from a-z.

        But when used in conjunction with the asterisk, *? means "zero or more of whatever came before the *?, but as few as possible. To quote the manual:
        c467b44d-56f8-4994-83e5-b94f2a90ee58-image.png

        I don’t have the .newline checked.

        Good. I still try to put (?-s) in such regex, because I cannot know what your checkbox is, and cannot know that you will remember to verify the checkbox some days later when you try it again.

        I 2 Replies Last reply Nov 15, 2022, 8:55 PM Reply Quote 3
        • I
          IanSunlun @PeterJones
          last edited by IanSunlun Nov 15, 2022, 8:55 PM Nov 15, 2022, 8:55 PM

          @PeterJones Actually sometimes the string I’m searching for is split up onto two lines.
          Can I use the .newline checkbox for that, or is there a better way of matching over a line break.

          e.g. My_Stringabcdefghi
          jklm12345"
          needs to be matched

          1 Reply Last reply Reply Quote 0
          • I
            IanSunlun @PeterJones
            last edited by IanSunlun Nov 15, 2022, 9:35 PM Nov 15, 2022, 9:30 PM

            @PeterJones To confirm, I just found out that the string I am searching for is sometimes spread over 2 lines.

            i.e. ThisIsTheStringIAmSearchingFor - this exact specific string - is normally on the same line. But sometimes its like this:
            ThisIsTheStringIAmSearch
            ingFor

            I dont know where the line break may occur, or even if a line break does occur. How do I make sure I find that string in the Find: box?

            P 1 Reply Last reply Nov 15, 2022, 9:50 PM Reply Quote 0
            • P
              PeterJones @IanSunlun
              last edited by PeterJones Nov 15, 2022, 9:50 PM Nov 15, 2022, 9:50 PM

              @IanSunlun ,

              You mean, we can no longer assume that My_String prefix will be all together? That makes it harder.

              If we could assume My_String prefix was always on the same line, then My_String((?s).*?)" would match, and everything (including the newline) would be in ${1} for the replacement.

              However, if we can no longer assume that, we will have to have one capture group per letter, with an optional newline between every single character

              • FIND = (T)\R?(h)\R?(i)\R?(s)\R?(I)\R?(s)\R?(T)\R?(h)\R?(e)\R?(S)\R?(t)\R?(r)\R?(i)\R?(n)\R?(g)\R?(I)\R?(A)\R?(m)\R?(S)\R?(e)\R?(a)\R?(r)\R?(c)\R?(h)\R?(i)\R?(n)\R?(g)\R?(F)\R?(o)\R?(r)\R?"
              • REPLACE = ${1}${2}${3}...${29}${30}.mhtml"
                (I did not show all thirty ${N}… I hope you can figure out what goes in the …)

              I think @Alan-Kilborn has a script he published a few months back that does a “space insensitive” search, but i am having trouble finding it. I hope he can post a link to it, because I think that might work better for you.

              A 1 Reply Last reply Nov 15, 2022, 9:58 PM Reply Quote 2
              • A
                Alan Kilborn @PeterJones
                last edited by Nov 15, 2022, 9:58 PM

                @PeterJones said in Regex: How do I search for My_String [any characters] [quotation mark] ?:

                I think @Alan-Kilborn has a script he published a few months back that does a “space insensitive” search

                HERE is the requested script (only 3 weeks old, not months).

                I 1 Reply Last reply Nov 16, 2022, 4:40 PM Reply Quote 3
                • I
                  IanSunlun @Alan Kilborn
                  last edited by IanSunlun Nov 16, 2022, 4:41 PM Nov 16, 2022, 4:40 PM

                  @Alan-Kilborn and @PeterJones
                  thanks, I installed that and it appears its working for me.

                  To add a little complication into the mix, I am searching in .mhtml files.
                  These files have a habit of putting an ‘=’ (equals sign) sign at the end of many lines. Is there anything I can add to the search term to include the possibility of finding an equals sign in the midst of a search phrase ?

                  So, I might get: MyStringabcdef=
                  ghijklm12345

                  A 1 Reply Last reply Nov 16, 2022, 6:24 PM Reply Quote 0
                  • A
                    Alan Kilborn @IanSunlun
                    last edited by Nov 16, 2022, 6:24 PM

                    @IanSunlun

                    Is there anything I can add to the search term to include the possibility of finding an equals sign in the midst of a search phrase ?

                    I suppose you could change the script part where it uses r'\h*' to be r'[\h=]* instead…

                    I 1 Reply Last reply Nov 16, 2022, 6:53 PM Reply Quote 1
                    • I
                      IanSunlun @Alan Kilborn
                      last edited by IanSunlun Nov 16, 2022, 6:54 PM Nov 16, 2022, 6:53 PM

                      @Alan-Kilborn I’ll give that a try.
                      What happens if I want to keep some of what I find in the search string in the replacement string?

                      E.g. lets say I find:
                      MyStringAbcd=
                      efg12345xyz

                      And I want to replace it with:
                      MyNewStringAbcdefg99999xyz

                      Is it possible to store in, for example, ${1} the Abcdefg ?

                      A N 2 Replies Last reply Nov 16, 2022, 7:03 PM Reply Quote 0
                      • A
                        Alan Kilborn @IanSunlun
                        last edited by Alan Kilborn Nov 16, 2022, 7:03 PM Nov 16, 2022, 7:03 PM

                        @IanSunlun said in Regex: How do I search for My_String [any characters] [quotation mark] ?:

                        What happens if I want to keep some of what I find in the search string in the replacement string?

                        I guess that could get tricky.
                        I don’t have all the answers. :-(
                        I suppose it is something you have to experiment with, see if you can make it do what you want it to.

                        1 Reply Last reply Reply Quote 1
                        • N
                          Neil Schipper @IanSunlun
                          last edited by Nov 16, 2022, 8:16 PM

                          @IanSunlun If you’re confident a trailing equal sign =\R won’t ever appear in a context where you want to preserve it, then do a pass where you just blow them all away (before the pass where you do other text replacement).

                          I 1 Reply Last reply Nov 16, 2022, 8:21 PM Reply Quote 1
                          • I
                            IanSunlun @Neil Schipper
                            last edited by Nov 16, 2022, 8:21 PM

                            @Neil-Schipper thanks, yes, I just did that a few mins ago.
                            I was concerned that .mhtml file was limiting line length to 76 chars for a reason.
                            I blew the =\r\n away for all the files, and they load up in the browser OK, so hopefully the = was not critical.

                            P 1 Reply Last reply Nov 16, 2022, 8:33 PM Reply Quote 0
                            • P
                              PeterJones @IanSunlun
                              last edited by PeterJones Nov 16, 2022, 8:35 PM Nov 16, 2022, 8:33 PM

                              @IanSunlun ,

                              Wait, 72 character line length and = ending a line. I think your .mhtml might be using Quoted Printable . The = at the end of a line does have a meaning in QP.

                              You really need to learn the file types you are editing before you start hacking around at things willy-nilly. Good luck getting that .mhtml to work right after all your edits.

                              I 1 Reply Last reply Nov 16, 2022, 8:43 PM Reply Quote 1
                              • I
                                IanSunlun @PeterJones
                                last edited by IanSunlun Nov 16, 2022, 8:44 PM Nov 16, 2022, 8:43 PM

                                @PeterJones
                                I’m trying to download my website for offline viewing. I downloaded .mhtml files. There seemed to be no other option for .mhtml saving - the default seems to have been this Quoted Printable format you mentioned.

                                I took out all the =\r\n episodes in all the .mhtml files and loaded them back into my browser.
                                Nothing seems to have changed. Its browsing and viewing them all fine. Now my other search and replace tasks will be easier.
                                All I am needing is offline browsing.

                                P 1 Reply Last reply Nov 16, 2022, 8:46 PM Reply Quote 0
                                • P
                                  PeterJones @IanSunlun
                                  last edited by Nov 16, 2022, 8:46 PM

                                  @IanSunlun ,

                                  I just checked; yes, saving a webpage as .mhtml from a major browser will use quoted-printable.

                                  If you really want to correctly do this:
                                  b51566b2-3818-44da-ae91-9e391acbcc46-image.png

                                  1. Select one part (the HTML part of the multipart, for example)
                                    7ba2d2b6-b23b-4324-9aa4-9dffc06a5b90-image.png
                                  2. Plugins > MIME Tools > Quoted Printable Decode
                                    38b62dc5-c946-4a92-a1d1-2a8f1a709ea0-image.png
                                  3. Do your search/replace
                                  4. Select all that text again
                                  5. Plugins > MIME Tools > Quoted Printable Encode

                                  You will find it’s much easier to work with that way.

                                  I 1 Reply Last reply Nov 16, 2022, 8:49 PM Reply Quote 1
                                  • I
                                    IanSunlun @PeterJones
                                    last edited by Nov 16, 2022, 8:49 PM

                                    @PeterJones Ah, thats good to know, thanks !

                                    1 Reply Last reply Reply Quote 0
                                    11 out of 18
                                    • First post
                                      11/18
                                      Last post
                                    The Community of users of the Notepad++ text editor.
                                    Powered by NodeBB | Contributors