• Login
Community
  • Login

regex working on net page is not working in npp

Scheduled Pinned Locked Moved General Discussion
10 Posts 4 Posters 3.4k 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
    V S Rawat
    last edited by Feb 3, 2019, 8:03 PM

    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.

    A 1 Reply Last reply Feb 3, 2019, 8:27 PM Reply Quote 0
    • A
      Alan Kilborn @V S Rawat
      last edited by Feb 3, 2019, 8:27 PM

      @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
      • A
        Alan Kilborn
        last edited by Feb 3, 2019, 8:55 PM

        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.

        M 1 Reply Last reply Feb 3, 2019, 9:26 PM Reply Quote 3
        • M
          Meta Chuh moderator @Alan Kilborn
          last edited by Meta Chuh Feb 3, 2019, 9:28 PM Feb 3, 2019, 9:26 PM

          @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
            V S Rawat
            last edited by Feb 4, 2019, 8:14 AM

            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
              V S Rawat
              last edited by Feb 4, 2019, 11:26 AM

              @ 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.

              A 1 Reply Last reply Feb 4, 2019, 1:08 PM Reply Quote 0
              • A
                Alan Kilborn @V S Rawat
                last edited by Feb 4, 2019, 1:08 PM

                @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
                • A
                  Alan Kilborn
                  last edited by Feb 4, 2019, 1:09 PM

                  @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
                  • G
                    guy038
                    last edited by Feb 4, 2019, 2:43 PM

                    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
                      V S Rawat
                      last edited by Feb 4, 2019, 7:51 PM

                      yes, it could find the correct part.

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

                      Thanks a gig, dear sir. :-)

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