#include <MsgBoxConstants.au3> ; to declare the Constants of MsgBox
Local $sRegex = "(?m)(?<=\bhttps?://\S+/)[^\s?#/]+(?![^\s?#])"
Local $sString = "For any of the following URLs, I'm looking for a match for segment as follows:" & @CRLF & _
"" & @CRLF & _
" https://example.com/what-not/segment matches segment" & @CRLF & _
" https://example.com/what-not/segment?a=b matches segment" & @CRLF & _
" https://example.com/what-not/segment#a matches segment" & @CRLF & _
"" & @CRLF & _
"In case there is a match for segment, I'm going to replace it with segment/." & @CRLF & _
"" & @CRLF & _
"For any of the following URLs, there should be no match:" & @CRLF & _
"" & @CRLF & _
" https://example.com/what-not/segment/ no match" & @CRLF & _
" https://example.com/what-not/segment/?a=b no match" & @CRLF & _
" https://example.com/what-not/segment/#a no match" & @CRLF & _
""
Local $sSubst = "$1segment/"
Local $sResult = StringRegExpReplace($sString, $sRegex, $sSubst)
MsgBox($MB_SYSTEMMODAL, "Result", $sResult)
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for AutoIt, please visit: https://www.autoitscript.com/autoit3/docs/functions/StringRegExp.htm