正则表达式:(#)和(?或&或\ n)之间的所有内容

时间:2019-07-23 08:26:21

标签: regex

需要捕获

  

https://my-stite.com/sec/pics/page.html#home

     

https://my-stite.com/sec/pics/page.html#manage

     

https://my-stite.com/sec/pics/page.html#proc-PO&/editProc/

     

https://my-stite.com/sec/pics/page.html#proc-PO?/abcse

结果应为

home
manage
proc-PO
proc-PO

尝试(。)(?<=#)(。?)(?= \ n | \?| \&)(。* | \ n),但不会捕获新行

3 个答案:

答案 0 :(得分:1)

尝试以下正则表达式模式:

(?<=#).*?(?=(?:[?&\n]|$))

Demo

以下是该模式的说明:

(?<=#)            assert that a pound sign immediately preceeds
.*?               then match everything until
(?=(?:[?&\n]|$))  seeing either ?, &, \n OR the end of the input

请注意将输入的结束作为停止条件进行检查。这涵盖了如下情况的URL恰好是输入中的最后一个URL:

https://my-stite.com/sec/pics/page.html#manage

在这种情况下,?&\n都不匹配,但我们仍要捕获文本manage

答案 1 :(得分:1)

尝试一下:

#\K[^?&\n]+

Here Is Demo

答案 2 :(得分:1)

只需投入两美分即可:

#([^/\s&?]+)

抓住第一组,然后查看a demo on regex101.com

相关问题