Jump to content

Recommended Posts

Posted (edited)

Olá, é tenho um rich com um código fonte e preciso criar um método automático de busca de link, as regras são as seguintes, o link sempre irá terminar em "/previsao.avi" e o início é "http://", ou seja, não tem como saber quantos caracteres são porque varia, só o inicio e o fim do link serao sempre iguais.

Por exemplo, uma hora o link pode ser "http://www.algumacoisa.com/32832/previsao.avi" outra hora pode ser "http://www.outracoisa.com/test/2933/previsao.avi". Não estou conseguindo pegar esse link. E quando conseguir, tem algum método de baixar esse arquivo ".avi" que está no link?

Obrigado desde já

Edited by User LoL
Posted (edited)

Boas,

podes usar regular expressions para extrair os links.

Exemplo:

Dim text As String = "texto com os links"
Dim listaEnderecos As New List(Of String)
'
Dim regx As New Regex("http://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?", RegexOptions.IgnoreCase)
Dim matches As MatchCollection = regx.Matches(text)
For Each match As Match In matches
   listaEnderecos.Add(match.Value)
Next

A lista listaEnderecos vai conter os links extraidos

Edited by nelsonr
  • Vote 1
Posted

Obrigado. Agora so tenho mais uma dúvida, não estou conseguindo usar o Regular Expressions para selecionar um valor entre uma tag. Preciso pegar o texto que estiver depois de " <PRE CLASS="western"> " e antes de " </PRE> ".

Tentei assim, mas não deu certo:

Dim text As String = RichTextBox1.Text
	Dim listaEnderecos As New List(Of String)
	'
	Dim regx As New Regex("<PRE CLASS=""western"">([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)</PRE>", RegexOptions.IgnoreCase)
	Dim matches As MatchCollection = regx.Matches(text)
	For Each match As Match In matches
		listaEnderecos.Add(match.Value)
		ListBox1.Items.Add(match.Value)
	Next
Posted

Podes fazer algo assim:

Dim listaConteudo As New List(Of String)
Dim regx As New Regex("<PRE[a-zA-Z ""=]*>(?<content>.*?)<\/PRE>")
Dim matches As MatchCollection = regx.Matches(RichTextBox1.Text)
For Each match As Match In matches
   listaConteudo.Add(match.Groups("content").Value)
Next
Posted

Não está funcionando. Não sei se leva em consideração, mas o texto é multiline, ou seja, o <PRE começa em uma linha, e o </PRE> termina em outra linha, eu preciso passar pra um rich o texto que estiver no meio

Posted

Se tem mudança de linha, experimenta com esta pattern

Dim listaConteudo As New List(Of String)
Dim regx As New Regex("<PRE[a-zA-Z ""=]*>(?<content>(.|\n)*?)<\/PRE>")
Dim matches As MatchCollection = regx.Matches(texto)

For Each match As Match In matches
   listaConteudo.Add(match.Groups("content").Value)
Next

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
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.