Jump to content
youtuber

How to extract word from url with regex?

Recommended Posts

youtuber

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

 

Share this post


Link to post
Share on other sites
ViciousXUSMC

Getting that out of the URL would be easy, but the real question is what is the full scope of the code.

How do you expect it to work on other URLs?  what is going to be the common capture group you need?

Share this post


Link to post
Share on other sites
youtuber

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

 

Share this post


Link to post
Share on other sites
mikell

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

  • Like 1

Share this post


Link to post
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

×