Jump to content

Regex help please


Recommended Posts

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

Posted (edited)

here is my approach  (partially)

; 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 by ioa747
corection

I know that I know nothing

Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...