regex working on net page is not working in npp



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



  • @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?



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



  • @Alan-Kilborn

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

    (s.h) 😉👍



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



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



  • @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/



  • @V-S-Rawat said:

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

    It doesn’t.



  • 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



  • yes, it could find the correct part.

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

    Thanks a gig, dear sir. :-)


Log in to reply