Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match part of url with regex

Tags:

regex

I have a challenge with a regex match to a url I hope I can bug some of you clever heads with :-)

Please take a look at this testcase https://www.regex101.com/r/bH4hE1/2

I use the regex: (\w+)(.\w+)+(?!.*(\w+)(.\w+)+)

Problem is, it only finds reports.html but I also need to find reports in the first url

https://my.website.com/reports?ref_=kdp_BS

https://my.website.com/reports.html

like image 221
Dyvel Avatar asked Oct 29 '25 20:10

Dyvel


1 Answers

To capture "reports" or "reports.html" in any path, begin your match after the last /, and capture word characters and .:

/.*\/([.\w+]+)/

See: https://www.regex101.com/r/iZ7dF3/8

like image 190
Ben Grimm Avatar answered Oct 31 '25 11:10

Ben Grimm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!