parse links

Get help with using AutoHotkey and its commands and hotkeys
fedek
Posts: 60
Joined: 17 May 2016, 12:17
Facebook: nope
Google: nope
GitHub: nope

parse links

31 Jul 2017, 12:26

How to parse links like "https://vk.com/" or "https://vk.com/feffe" including. How to stop parse behind the space?
Vh_
Posts: 201
Joined: 17 Mar 2017, 22:06

Re: parse links

31 Jul 2017, 18:36

Not sure I understand your request. Can you give an example?-
fedek
Posts: 60
Joined: 17 May 2016, 12:17
Facebook: nope
Google: nope
GitHub: nope

Re: parse links

01 Aug 2017, 06:46

Example:
Hi, bla bla bla this link https://vk.com
So, bla bla bla bla https://vk.com/omg1 this group.
End of example
result1 should be "https://vk.com"
result2 should be "https://vk.com/omg1".
Something like this maybe:
while cPos := RegExMatch(text, "(\Qhttps://vk.com\E.*[A-Za-z0-9\-_]) end here if space or end of the line ", link,cPos?cPos+1:1)
BoBo
Posts: 3764
Joined: 13 May 2014, 17:15

Re: parse links

01 Aug 2017, 06:57

SplitPath :?:
User avatar
thenasos
Posts: 79
Joined: 10 Nov 2015, 10:00

Re: parse links

01 Aug 2017, 08:57

Something like this?

Code: Select all

FileRead, SomeText, somefile.txt
StringSplit,SplitText,SomeText,`n,`r
i := 0

Loop, % SplitText0
{
i++
if (RegExMatch(SplitText%i%,"((w{3}.)|(https?))(:\/{2})?\S+", foundURL))
	listUpdate .= foundURL "`r"
}
MsgBox, %  listUpdate
return
Pretty sure, can be written better...
Shrek is love...
garry
Posts: 2433
Joined: 22 Dec 2013, 12:50

Re: parse links

01 Aug 2017, 09:44

thank you thenasos , works fine
how can I find all url's ( more then one url per line ) ?
Hi, bla bla bla this link http://vk1.com xxxx https://vk2.com yyyyy
So, bla bla bla bla https://vk1.com/omg1 aaaaa https://vk1.com/omg2 this group.
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: parse links

01 Aug 2017, 09:54

It's a bit more complex, but for anyone who doesn't like regular expressions here's a way to do it without them; it also handles multiple urls per line.

Code: Select all

$^m::
	TextToParse := "Hi, bla bla bla this link http://vk1.com xxxx https://vk2.com yyyyy`nSo, bla bla bla bla https://vk1.com/omg1 aaaaa https://vk1.com/omg2 this group."
	
	ResultArray := []

	loop
	{
		Pos := InStr(TextToParse, "https://")
		Pos2 := InStr(TextToParse, "http://")
		if (Pos && Pos2)
			TextToParse := SubStr(TextToParse, (Pos < Pos2 ? Pos : Pos2))
		else if (Pos)
			TextToParse := SubStr(TextToParse, Pos)
		else if (Pos2)
			TextToParse := SubStr(TextToParse, Pos2)
		else
			break
		
		Pos3 := InStr(TextToParse, " ")
		Pos4 := InStr(TextToParse, "`n")
		if (Pos3 && Pos4)
			SplitText := SubStr(TextToParse, 1, (Pos3 < Pos4 ? Pos3-1 : Pos4-1))
		else if Pos3
			SplitText := SubStr(TextToParse, 1, Pos3-1)
		else if Pos4
			SplitText := SubStr(TextToParse, 1, Pos4-1)
		else
			SplitText := TextToParse
		ResultArray[A_Index] := SplitText
		
		TextToParse := SubStr(TextToParse, StrLen(SplitText)+1)
	}

	for key, value in ResultArray
		ResultMessage .= value . "`n"
		
	MsgBox % ResultMessage
	return
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: parse links

01 Aug 2017, 10:03

Here's one way:

Code: Select all

q:: ;extract urls from text
vText := " ;continuation section
(
Hi, bla bla bla this link https://vk.com
So, bla bla bla bla https://vk.com/omg1 this group.
End of example
)"

vText := RegExReplace(vText, "[`r ]", "`n") "`n" ;replace non-url characters with LFs (an LF is appended for when we remove lines later, in case the string ends with a url)
vText := StrReplace(vText, "http", "`nhttp")
vText := RegExReplace(vText, "`nm)(^http.*`n)|^.*`n", "$1") ;remove any line that doesn't start with http
vText := StrReplace(vText, "`n", "`r`n")
Clipboard := vText
MsgBox, % "done"
return
Last edited by jeeswg on 01 Aug 2017, 10:41, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
AlphaBravo
Posts: 491
Joined: 29 Sep 2013, 22:59

Re: parse links

01 Aug 2017, 10:36

garry wrote:thank you thenasos , works fine
how can I find all url's ( more then one url per line ) ?
Hi, bla bla bla this link http://vk1.com xxxx https://vk2.com yyyyy
So, bla bla bla bla https://vk1.com/omg1 aaaaa https://vk1.com/omg2 this group.

Code: Select all

h =
(
Hi, bla bla bla this link http://vk1.com xxxx https://vk2.com yyyyy
So, bla bla bla bla https://vk1.com/omg1 aaaaa https://vk1.com/omg2 this group.
)

while pos := RegExMatch(H, "https?://\S+", m, A_Index=1?1:pos+StrLen(m))
	MsgBox % m
User avatar
thenasos
Posts: 79
Joined: 10 Nov 2015, 10:00

Re: parse links

02 Aug 2017, 01:13

garry wrote:thank you thenasos , works fine
how can I find all url's ( more then one url per line ) ?
Hi, bla bla bla this link http://vk1.com xxxx https://vk2.com yyyyy
So, bla bla bla bla https://vk1.com/omg1 aaaaa https://vk1.com/omg2 this group.
For my example, just change this line, to enambe multiple urls in a single line:

Code: Select all

StringSplit,SplitText,SomeText,`n `t,`r
Shrek is love...
garry
Posts: 2433
Joined: 22 Dec 2013, 12:50

Re: parse links

02 Aug 2017, 14:16

sorry for late answer
thank you all for help and the examples , they work fine
this was my old example :

Code: Select all

;- search links only with specified extensions
transform,s,chr,32
h =
(
Hi, bla bla bla this link "http://www.vk1/test.mp4" xxxx https://vk2.com yyyyy
So, bla bla bla bla https://vk3/xy.com aaaaa https://vk2/test.mp3 this group.
)

A1:="http"                                                  ;- begin
Extensions1 := "mp3,m4a,wav,wma,flv,vob,mp4,mpg,wmv,avi"    ;- end

Loop,parse,h,`n,`r
{
stringlen,L1,A1
Loop, Parse,A_LoopField,%s%`"
  {
  StringLeft,R1,A_LoopField,L1
  If (R1=A1)
    {
    SplitPath,A_LoopField, name, dir, ext, name_no_ext, drive
    if ext in %Extensions1%
      {
      e .= A_LoopField "`n"
      Last=%A_LoopField%
      }
    }
  }
}
msgbox,LINKS=`n%e%`nLAST=`n%last%
return

Return to “Ask For Help”

Who is online

Users browsing this forum: Googleye, jarhead, keylo, mikeyww, roonyroox, songdg, william_ahk and 49 guests