Jump to content

How to extract word from url with regex?


youtuber
 Share

Recommended Posts

Friends I want to get /autoit" or /autoit/ word from this source

Local $RegexSource[3] = ['href="https://www.autoitscript.com/forum/autoit/forum/" />', 'href="https://www.autoitscript.com/forum/autoit" />', 'href="https://www.autoitscript.com/forum/autoit" title="']

my regex pattern

$arrayregex = StringRegExp($RegexSource[$i], 'href="' & $aRegexDomain[0] & '.*forum/(.*?)(/[^/|"]+)', 3)

And my codes

Local $RegexSourceDomainName[3] = ['https://www.autoitscript.com/forum/', 'https://www.autoitscript.com/forum/autoit', 'https://www.autoitscript.com/']
Local $RegexSource[3] = ['href="https://www.autoitscript.com/forum/autoit/forum/" />', 'href="https://www.autoitscript.com/forum/autoit" />', 'href="https://www.autoitscript.com/forum/autoit" title="']

For $i = 0 To UBound($RegexSource) - 1
    $aRegexDomain = StringRegExp($RegexSourceDomainName[$i], 'https?://[^/]+', 3)
    $arrayregex = StringRegExp($RegexSource[$i], 'href="' & $aRegexDomain[0] & '.*forum/(.*?)(/[^/|"]+)', 3)

    If IsArray($arrayregex) Then
        ConsoleWrite($aRegexDomain[0] & " : " &$arrayregex[0] & @CRLF)
    EndIf
Next

and my console output

NJCctFs1QqyA169hQ3byfg.png

 

Link to comment
Share on other sites

Obviously this sees my work but this method is too long I want to do with Regex

#include <string.au3>
Local $RegexSourceDomainName[3] = ['https://www.autoitscript1.com/forum/', 'https://www.autoitscript2.com/forum/autoit', 'https://www.autoitscript3.com/']
Local $RegexSource[3] = ['href="https://www.autoitscript1.com/forum/autoit1/forum/" />', 'href="https://www.autoitscript2.com/forum/autoit2" />', 'href="https://www.autoitscript3.com/forum/autoit3" title="']

For $i = 0 To UBound($RegexSource) - 1
    $aRegexDomain = StringRegExp($RegexSourceDomainName[$i], 'https?://[^/]+', 3)
    $arrayregex = _StringBetween($RegexSource[$i], 'href="' & $aRegexDomain[0] & '/forum/','"')

    If IsArray($arrayregex) Then
        $arrayregex = StringReplace($arrayregex[0], "/forum/", "")
        ConsoleWrite($aRegexDomain[0] & " : " & $arrayregex & @CRLF)
        EndIf
Next

 

Link to comment
Share on other sites

This shortens the code while getting the same results

#include <string.au3>
;~ Local $RegexSourceDomainName[3] = ['https://www.autoitscript1.com/forum/', 'https://www.autoitscript2.com/forum/autoit', 'https://www.autoitscript3.com/']
Local $RegexSource[3] = ['href="https://www.autoitscript1.com/forum/autoit1/forum/" />', 'href="https://www.autoitscript2.com/forum/autoit2" />', 'href="https://www.autoitscript3.com/forum/autoit3" title="']

For $i = 0 To UBound($RegexSource) - 1
    $s = StringRegExpReplace($RegexSource[$i], '(https?://[^/]+)/forum/' & '([^/"]+).*', "$1 : $2")
    ConsoleWrite($s & @CRLF)

;~     $aRegexDomain = StringRegExp($RegexSourceDomainName[$i], 'https?://[^/]+', 3)
;~     $arrayregex = _StringBetween($RegexSource[$i], 'href="' & $aRegexDomain[0] & '/forum/','"')

;~     If IsArray($arrayregex) Then
;~         $arrayregex = StringReplace($arrayregex[0], "/forum/", "")
;~         ConsoleWrite($aRegexDomain[0] & " : " & $arrayregex & @CRLF)
;~         EndIf
Next

But as said ViciousXUSMC  I really don't know how this could be used for a more general purpose

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...