Community
    • Login

    regex working on net page is not working in npp

    Scheduled Pinned Locked Moved General Discussion
    10 Posts 4 Posters 3.2k 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.
    • V S RawatV
      V S Rawat
      last edited by

      at a website, a user asked he wanted to get the content between second-to-last and last forward slash, or so I understood.
      i.e, from “https://www.facebook.com/pages/Something/976313416535/”, he wanted 976313416535 (which may or may not be numbers in other cases, I guess)

      I tested on https://regex101.com/, this regex will find the last set of numbers.

      ^.*\/(.*)\/$
      

      meaning, replacing it with

       \1
      

      will give the last desired string.

      It is working there for all (PCRE (PHP), ECMAScript, (JavaScript), Python, Golang) but it refused to work within notepad+ find replace, saying that it couldn’t find any match (with regular expression ticked).

      What did I miss? How is npp regex different from all above regex?

      Thanks.

      Alan KilbornA 1 Reply Last reply Reply Quote 0
      • Alan KilbornA
        Alan Kilborn @V S Rawat
        last edited by

        @V-S-Rawat

        That find/replace regex pair works fine for me on Notepad++, although one would do well to note that you do not need either of the \ that you show there. As to why it isn’t working for you…maybe show a screenshot of your Replace window?

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

          Clarification:

          note that you do not need either of the \

          I meant the two \ in the Find expression, not the single one in the Replace expression – that one is totally necessary.

          Meta ChuhM 1 Reply Last reply Reply Quote 3
          • Meta ChuhM
            Meta Chuh moderator @Alan Kilborn
            last edited by Meta Chuh

            @Alan-Kilborn

            not the single one in the Replace expression – that one is totally necessary.

            (s.h) 😉👍

            1 Reply Last reply Reply Quote 2
            • V S RawatV
              V S Rawat
              last edited by

              oh. so, forward slash / is not a control character in regex and it need not be escaped.

              good enough. I got it now for ever.

              however, escaping a non-control character also should not change the behavior of regex. It sort of double emphasizes that the next letter is to be taken literally, not in its control meaning, just in case it has any control meaning I am not aware of.

              the above expression as well as the revised expression

              ^.*/(.*)/$
              

              both are selecting entire single line from start to end and saying "found 1st occurrence, reached end of file, there is a single line as above in the test text file, that is with or without crlf at the end of line, in case that is required to mark $ end-of-line.

              screenshot

              Thanks.

              1 Reply Last reply Reply Quote 1
              • V S RawatV
                V S Rawat
                last edited by

                @ Alan Kilborn

                No, when I removed those two \ from find, the regex is not giving error in npp, but then the regex is not working on regex101 site.

                Now I am sure that the site’s regex and npp’s regex are different.

                Alan KilbornA 1 Reply Last reply Reply Quote 0
                • Alan KilbornA
                  Alan Kilborn @V S Rawat
                  last edited by

                  @V-S-Rawat said:

                  Now I am sure that the site’s regex and npp’s regex are different.

                  This is true; not sure why you thought they would be the same. There are many different regex “engines”. For some examples of this, look around this site and its discussions: https://www.regular-expressions.info/

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

                    @V-S-Rawat said:

                    however, escaping a non-control character also should not change the behavior of regex

                    It doesn’t.

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

                      Hello, @v-s-rawat, @alan-kilborn and all,

                      To select any area, even empty, between the penultimate slash and the last slash of an Internet address, you could use the search regex, below :

                      SEARCH /\K[^/]*(?=/[^/]*$)

                      Just test it against the sample text below :

                      https://www.mysite.com/Something/976313416535/Text
                      https://www.mysite.com/Something//Text
                      https://www.mysite.com/Something/976313416535/
                      https://www.mysite.com/Something//
                      https://www.mysite.com/Something/976313416535
                      https://www.mysite.com/Something/97//631/34/abcde/
                      https://www.mysite.com/Something/97//631/34/abcde//
                      https://www.mysite.com/Something/97//631/34/abcde/12345
                      

                      Note :

                      When doing a replacement ( or a suppression ), with the search regex, above, remember that you must use the Replace All button, exclusively ;-)) The step-by-step replacement, with the Replace button does not work, due to the \K syntax :-((

                      Best Regards

                      guy038

                      1 Reply Last reply Reply Quote 4
                      • V S RawatV
                        V S Rawat
                        last edited by

                        yes, it could find the correct part.

                        /\K[^/]*(?=/[^/]*$)
                        

                        Thanks a gig, dear sir. :-)

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