Hola, me gustaría saber qué hacer para encontrar una cadena de texto dentro de un párrafo de texto más extenso en Windows Forms, por ejemplo, yo ingreso lo siguiente en un textBox:
<a href="http://1.bp.blogspot.com/-NhL7eyZF_bM/UC6AO7LanyI/AAAAAAAADNw/VkfXa-fNxpA/s1600/glucides-vitamines-fruits.jpg"/> <a href="http://1.bp.blogspot.com/-NhL7eyZF_bM/UC6AO7LanyI/AAAAAAAADNw/VkfXa-fNxpA/s1600/glucides-vitamines-fruits11.jpg"/>
y lo que me gustaría encontrar para que lo mostrara en otro textBox es solamente http://1.bp.blogspot.com/-NhL7eyZF_bM/UC6AO7LanyI/AAAAAAAADNw/VkfXa-fNxpA/s1600/glucides-vitamines-fruits.jpg y http://1.bp.blogspot.com/-NhL7eyZF_bM/UC6AO7LanyI/AAAAAAAADNw/VkfXa-fNxpA/s1600/glucides-vitamines-fruits11.jpg
omitiendo <a href=" y "/> contando que podrían agregarse muchos más códigos con links en el mismo textBox. Espero haberme explicado y me puedan ayudar.
Qué lenguaje estás manejando?
Saludos.
Hay varias formas.
Si estás usando un html/xml/xmlns lo mejor quizás sería que uses htmlagilitypack: http://htmlagilitypack.codeplex.com/
...Pero es el método más dificil de entre los que existen, y dependiendo del conteido (sopa de tags) podría no serte útil en absoluto.
Puedes usar el método SPLIT : http://msdn.microsoft.com/en-us/library/system.string.split.aspx
for each item in variable_de_tipo_String.split(controlchars.quote) : msgbox(item) : next
O mi manera favorita, Expresiones regulares: http://en.wikipedia.org/wiki/Regular_expression
Output:
http://1.bp.blogspot.com/-NhL7eyZF_bM/UC6AO7LanyI/AAAAAAAADNw/VkfXa-fNxpA/s1600/glucides-vitamines-fruits.jpg
http://1.bp.blogspot.com/-NhL7eyZF_bM/UC6AO7LanyI/AAAAAAAADNw/VkfXa-fNxpA/s1600/glucides-vitamines-fruits11.jpg
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim str As String = <a><![CDATA[<a href="http://1.bp.blogspot.com/-NhL7eyZF_bM/UC6AO7LanyI/AAAAAAAADNw/VkfXa-fNxpA/s1600/glucides-vitamines-fruits.jpg"/> <a href="http://1.bp.blogspot.com/-NhL7eyZF_bM/UC6AO7LanyI/AAAAAAAADNw/VkfXa-fNxpA/s1600/glucides-vitamines-fruits11.jpg"/>]]></a>.Value
Dim regex As String = <a><![CDATA[(http://|https://|www)([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?]]></a>.Value
For Each match In RegEx_Matches_To_List(str, regex) : MsgBox(match) : Next
End Sub
#Region " RegEx Matches To List "
' [ RegEx Matches To List Function ]
'
' // By Elektro H@cker
Private Function RegEx_Matches_To_List(ByVal str As String, ByVal RegEx_Pattern As String, _
Optional ByVal Group As Int32 = 0, _
Optional ByVal IgnoreCase As Boolean = True) _
As List(Of String)
Dim regex_option As System.Text.RegularExpressions.RegexOptions
If IgnoreCase Then regex_option = System.Text.RegularExpressions.RegexOptions.IgnoreCase _
Else regex_option = System.Text.RegularExpressions.RegexOptions.None
Dim match As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(str, RegEx_Pattern, regex_option)
Dim Matches_List As New List(Of String)
Do While match.Success
Matches_List.Add(match.Groups(Group).ToString)
match = match.NextMatch()
Application.DoEvents()
Loop
Return Matches_List
End Function
#End Region
End Class
Saludos.
Cita de: XresH en 7 Junio 2013, 10:37 AM
Qué lenguaje estás manejando?
Saludos.
C++ en Windows Forms.
Cita de: EleKtro H@cker en 7 Junio 2013, 10:41 AM
Hay varias formas.
Si estás usando un html/xml/xmlns lo mejor quizás sería que uses htmlagilitypack: http://htmlagilitypack.codeplex.com/
...Pero es el método más dificil de entre los que existen, y dependiendo del conteido (sopa de tags) podría no serte útil en absoluto.
Puedes usar el método SPLIT : http://msdn.microsoft.com/en-us/library/system.string.split.aspx
for each item in variable_de_tipo_String.split(controlchars.quote) : msgbox(item) : next
O mi manera favorita, Expresiones regulares: http://en.wikipedia.org/wiki/Regular_expression
Output:
http://1.bp.blogspot.com/-NhL7eyZF_bM/UC6AO7LanyI/AAAAAAAADNw/VkfXa-fNxpA/s1600/glucides-vitamines-fruits.jpg
http://1.bp.blogspot.com/-NhL7eyZF_bM/UC6AO7LanyI/AAAAAAAADNw/VkfXa-fNxpA/s1600/glucides-vitamines-fruits11.jpg
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim str As String = <a><![CDATA[<a href="http://1.bp.blogspot.com/-NhL7eyZF_bM/UC6AO7LanyI/AAAAAAAADNw/VkfXa-fNxpA/s1600/glucides-vitamines-fruits.jpg"/> <a href="http://1.bp.blogspot.com/-NhL7eyZF_bM/UC6AO7LanyI/AAAAAAAADNw/VkfXa-fNxpA/s1600/glucides-vitamines-fruits11.jpg"/>]]></a>.Value
Dim regex As String = <a><![CDATA[(http://|https://|www)([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?]]></a>.Value
For Each match In RegEx_Matches_To_List(str, regex) : MsgBox(match) : Next
End Sub
#Region " RegEx Matches To List "
' [ RegEx Matches To List Function ]
'
' // By Elektro H@cker
Private Function RegEx_Matches_To_List(ByVal str As String, ByVal RegEx_Pattern As String, _
Optional ByVal Group As Int32 = 0, _
Optional ByVal IgnoreCase As Boolean = True) _
As List(Of String)
Dim regex_option As System.Text.RegularExpressions.RegexOptions
If IgnoreCase Then regex_option = System.Text.RegularExpressions.RegexOptions.IgnoreCase _
Else regex_option = System.Text.RegularExpressions.RegexOptions.None
Dim match As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(str, RegEx_Pattern, regex_option)
Dim Matches_List As New List(Of String)
Do While match.Success
Matches_List.Add(match.Groups(Group).ToString)
match = match.NextMatch()
Application.DoEvents()
Loop
Return Matches_List
End Function
#End Region
End Class
Saludos.
Intenté entenderlo pero se me complicó, no sé muy bien de ese lenguaje y sé más de C++ en Windows Forms. Si pudieras ponerlo en el lenguaje que te menciono te lo agradecería.
Cita de: Juancho25 en 8 Junio 2013, 08:33 AM
Intenté entenderlo pero se me complicó, no sé muy bien de ese lenguaje y sé más de C++ en Windows Forms. Si pudieras ponerlo en el lenguaje que te menciono te lo agradecería.
Cita de: Elektro H@ckerDim regex As String = <a><![CDATA[(http://|https://|www)([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?]]></a>.Value
No, no sé C/C++/C#,
Copia ese RegEx, conviértelo a la sintaxis adecuada de C++ usando la aplicación "RegExBuddy", y ya tienes la primera parte del problema solucionada, el resto solo sería que aprendieses a usar las expresiones regulares en C++ (si no supieras).
Saludos