leuce Posted June 6 Share Posted June 6 Hello everyone I want to change this: <a href="some-folder/2021-abc/123-somefile-456"> into this: <a href="###123-somefile-456"> I.e. if the right-most folder's name (i.e. the part after the last forward slash) in the URL starts with a number, I want the hyperlink to contain only ### plus that folder's name. <a href="some-folder/2021-abc/somefile"> = must stay the same <a href="some-folder/2021-abc/somefile-123"> = must stay the same <a href="some-folder/2022-abc/1234"> must become: <a href="###1234"> <a href="some-folder/2022-abc/1234-somefile"> must become: <a href="###1234-somefile"> <a href="some-folder/2023-abc/123-somefile-456"> must become: <a href="###123-somefile-456"> <a href="some-folder/another-folder/2024-abc/789-somefile-456"> must become: <a href="###789-somefile-456"> This is the regular expression that I believe should work... but doesn't: $string = '<a href="some-folder/2021-abc/123-somefile-456">' $code = "###" $replace = StringRegExpReplace ($string, '(<a href=")(\S+?)(/[\S^/]+?">)', "$1" & $code & "$3") MsgBox (0, "", $replace, 0) Any advice? Thanks Samuel Link to comment Share on other sites More sharing options...
ioa747 Posted June 6 Share Posted June 6 (edited) here is my approach (partially) expandcollapse popup; https://www.autoitscript.com/forum/topic/211956-regex-help-please/?do=findComment&comment=1534523 Local $sTxt = "" $sTxt &= "<a href=""some-folder/2021-abc/somefile""> " & @CRLF $sTxt &= "<a href=""some-folder/2021-abc/somefile-123""> " & @CRLF $sTxt &= "<a href=""some-folder/2022-abc/1234""> " & @CRLF $sTxt &= "<a href=""some-folder/2022-abc/1234-somefile""> " & @CRLF $sTxt &= "<a href=""fake""> " & @CRLF $sTxt &= "<a href=""some-folder/2023-abc/123-somefile-456""> " & @CRLF $sTxt &= "<a href=""some-folder/another-folder/2024-abc/789-somefile-456"">" & @CRLF $sTxt &= "" & @CRLF Local $sFolder, $aData = StringSplit($sTxt, @CRLF, 0) For $n = 1 To $aData[0] If StringInStr($aData[$n], '<a href="') Then ;ConsoleWrite("$aData[$n]=" & $aData[$n] & @CRLF) $sFolder = f_GetHRef($aData[$n]) ConsoleWrite('<a href="' & $sFolder & '">' & @CRLF) EndIf Next Func f_GetHRef($sHtmlLine) Local $sRet = "", $aTemp = StringSplit($sHtmlLine, '"') For $n = 1 To $aTemp[0] If StringInStr($aTemp[$n - 1], '<a href=') Then If Not StringInStr($aTemp[$n], '/') Then ExitLoop $sRet = StringTrimLeft($aTemp[$n], StringInStr($aTemp[$n], '/', 0, -1)) ;ConsoleWrite("$sRet=" & $sRet ) If StringRegExp(StringLeft($sRet, 1), "\d+") Then $sRet = "###" & $sRet Else $sRet = $aTemp[$n] EndIf Return SetError(0, 0, $sRet) EndIf Next Return SetError(0, 1, $aTemp[$n]) EndFunc ;==>f_GetHRef Edited June 7 by ioa747 corection I know that I know nothing Link to comment Share on other sites More sharing options...
benners Posted June 6 Share Posted June 6 (edited) This perhaps? $replace = StringRegExpReplace($string, '<a href="[^/]+/[^/]+/(\d[^/]*?)">', '<a href="' & $code & '$1">') Or for more folder segments $replace = StringRegExpReplace($string, '<a href="(?:[^/]+/)*(\d[^/]*?)">', '<a href="' & $code & '$1">') Edited June 6 by benners ioa747 1 Link to comment Share on other sites More sharing options...
AspirinJunkie Posted June 7 Share Posted June 7 Another suggestion: Global $sTxt = _ '<a href="some-folder/2021-abc/somefile"> ' & @CRLF & _ '<a href="some-folder/2021-abc/somefile-123"> ' & @CRLF & _ '<a href="some-folder/2022-abc/1234"> ' & @CRLF & _ '<a href="some-folder/2022-abc/1234-somefile"> ' & @CRLF & _ '<a href="fake"> ' & @CRLF & _ '<a href="some-folder/2023-abc/123-somefile-456"> ' & @CRLF & _ '<a href="some-folder/another-folder/2024-abc/789-somefile-456">' & @CRLF $sProcessed = StringRegExpReplace($sTxt, '<a\b[^>]+href="\K[^"]+?\/(?=\d[^\/"]+")', '###') ConsoleWrite($sProcessed) ioa747 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now