elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Recopilación Tutoriales y Manuales Hacking, Seguridad, Privacidad, Hardware, etc


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP)
| | | |-+  Programación Visual Basic (Moderadores: LeandroA, seba123neo)
| | | | |-+  Extraer string entre "[" y "]"
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Extraer string entre "[" y "]"  (Leído 3,037 veces)
extreme69

Desconectado Desconectado

Mensajes: 178


Be BlackHat but don't forget your principles.


Ver Perfil
Extraer string entre "[" y "]"
« en: 17 Septiembre 2011, 01:35 am »

Tengo:

Código:
asd = aitheoiethi[BLABLABLA]taihoithaoihtoea

Y necesito "BLABLABLA" en la variable asd2.

Probé con Mid, Split, Left, Right, pero en todos los casos tengo que saber las posiciones, y no sé cuantos caracteres va a tener "BLABLABLA" ni cuantos caracteres hay antes ni después.

¿Será que primero tengo que ver en que posición del string está el "[", luego hacer lo mismo con el "]" y una vez que sé las posiciones hacer un mid? ¿o hay alguna manera más fácil/rápida?





« Última modificación: 17 Septiembre 2011, 01:37 am por extreme69 » En línea

DarkMatrix

Desconectado Desconectado

Mensajes: 150


Nuestro Limite es la Imaginacion


Ver Perfil WWW
Re: Extraer string entre "[" y "]"
« Respuesta #1 en: 17 Septiembre 2011, 01:51 am »

Código
  1. Public Function StrBetween(Cadena As String, SubCadena1 As String, Subcadena2 As String) As String
  2.  
  3.    Dim Pos1 As Integer, Pos2 As Integer
  4.  
  5.    Pos1 = InStr(UCase(Cadena), UCase(SubCadena1)) + 1
  6.    Pos2 = InStr(UCase(Cadena), UCase(Subcadena2))
  7.  
  8.    If Pos1 <> 0 And Pos2 <> 0 Then
  9.  
  10.        StrBetween = Mid$(Cadena, Pos1, Pos2 - Pos1)
  11.  
  12.    End If
  13.  
  14. End Function
  15.  
  16. Private Sub Form_Load()
  17.  
  18.    Dim ASd  As String
  19.    Dim Asd2 As String
  20.  
  21.    ASd = "aitheoiethi[BLABLABLAkjhuihui]taihoithaoihtoea"
  22.    Asd2 = StrBetween(ASd, "[", "]")
  23.  
  24.    MsgBox Asd2
  25.  
  26. End Sub

Por hay tambien hay una funcion que hizo Psyke1, espero que te sirva...


« Última modificación: 17 Septiembre 2011, 01:56 am por DarkMatrix » En línea

Todo aquello que no se puede hacer, es lo que no intentamos hacer.
Projecto Ani-Dimension Digital Duel Masters (Juego de cartas masivo multijugador online hecho en Visual Basic 6.0)

Desing by DarkMatrix
extreme69

Desconectado Desconectado

Mensajes: 178


Be BlackHat but don't forget your principles.


Ver Perfil
Re: Extraer string entre "[" y "]"
« Respuesta #2 en: 17 Septiembre 2011, 01:56 am »

Excelente, muchas gracias.  ;-)

En línea

BlackZeroX
Wiki

Desconectado Desconectado

Mensajes: 3.158


I'Love...!¡.


Ver Perfil WWW
Re: Extraer string entre "[" y "]"
« Respuesta #3 en: 17 Septiembre 2011, 04:03 am »

.

¿Será que primero tengo que ver en que posición del string está el "[", luego hacer lo mismo con el "]" y una vez que sé las posiciones hacer un mid? ¿o hay alguna manera más fácil/rápida?

que comes que adivinas...

Cita: http://visual-coders.herobo.com/blog/?p=1

Código
  1.  
  2. '------------------------------------------------------------------------
  3. ' *Function : Text_Between_Words
  4. ' *Author   : *PsYkE1*
  5. ' *Mail     : vbpsyke1@mixmail.com
  6. ' *Date     : 10/4/10
  7. ' *Purpose  : It returns the text wich is between two words
  8. ' *Recommended Websites :
  9. '       http://foro.rthacker.net/
  10. '       http://InfrAngeluX.Sytes.Net/
  11. '------------------------------------------------------------------------
  12. Option Explicit
  13. Public Function Text_Between_Words(ByVal sTextToAnalyze As String, ByVal sStartWord As String, ByVal sEndWord As String) As String
  14. Dim iPosition1                  As Integer
  15. Dim iPosition2                  As Integer
  16. Dim iStart                      As Integer
  17.    iPosition1 = InStr(sTextToAnalyze, sStartWord)
  18.    If CBool(iPosition1) Then
  19.        iStart = iPosition1 + Len(sStartWord)
  20.        iPosition2 = InStr(iStart, sTextToAnalyze, sEndWord)
  21.        If CBool(iPosition2) Then
  22.            Text_Between_Words = Mid$(sTextToAnalyze, iStart, iPosition2 - iStart)
  23.        End If
  24.    End If
  25. End Function
  26.  
  27.  

Código
  1.  
  2. Debug.Print Text_Between_Words("El contexto es el ámbito de referencia de un texto. ¿Qué entiendo por ámbito de referencia?.", "referencia", "referencia")
  3.  
  4.  

Dulces Lunas!¡.
« Última modificación: 17 Septiembre 2011, 04:07 am por BlackZeroX▓▓▒▒░░ » En línea

The Dark Shadow is my passion.
seba123neo
Moderador
***
Desconectado Desconectado

Mensajes: 3.621



Ver Perfil WWW
Re: Extraer string entre "[" y "]"
« Respuesta #4 en: 17 Septiembre 2011, 06:14 am »

Código
  1. Private Sub Form_Load()
  2. MsgBox TextoEntreMedio("aitheoiethi[BLABLABLA]taihoithaoihtoea", "[", "]")
  3. End Sub
  4.  
  5. Private Function TextoEntreMedio(Texto As String, Palabra1 As String, Palabra2 As String)
  6. TextoEntreMedio = Left$(Mid$(Texto, InStr(Texto, Palabra1) + Len(Palabra1)), InStr(Mid$(Texto, InStr(Texto, Palabra1) + Len(Palabra1)), Palabra2) - 1)
  7. End Function
En línea

BlackZeroX
Wiki

Desconectado Desconectado

Mensajes: 3.158


I'Love...!¡.


Ver Perfil WWW
Re: Extraer string entre "[" y "]"
« Respuesta #5 en: 17 Septiembre 2011, 06:49 am »


Código
  1.  
  2. Private Sub Form_Load()
  3. MsgBox TextoEntreMedio("aitheoiethi[BLABLABLA]taihoithaoihtoea", "(", "]")
  4. End Sub
  5.  
  6.  

Dulces Lunas!¡.
En línea

The Dark Shadow is my passion.
Psyke1
Wiki

Desconectado Desconectado

Mensajes: 1.089



Ver Perfil WWW
Re: Extraer string entre "[" y "]"
« Respuesta #6 en: 17 Septiembre 2011, 16:30 pm »

Un poco mejorada:
Código
  1. Option Explicit
  2.  
  3. Public Static Function TextBetweenWords$(ByRef sText$, ByVal sWord1$, ByVal sWord2$)
  4. Dim lPos1&, lPos2&, lStart&
  5.    If LenB(sText) Then
  6.        lPos1 = InStrB(1, sText, sWord1, vbBinaryCompare)
  7.        If lPos1 Then
  8.            lStart = lPos1 + LenB(sWord1)
  9.            lPos2 = InStrB(lStart, sText, sWord2, vbBinaryCompare)
  10.            If lPos2 Then
  11.                TextBetweenWords = MidB$(sText, lStart, lPos2 - lStart)
  12.            End If
  13.        End If
  14.    End If
  15. End Function
  16.  
  17.  
  18. Private Sub Form_Load()
  19.    Debug.Print TextBetweenWords("qwertysdfcv [raul338 es feo] prueba", "[", "]")
  20. End Sub

Devuelve:
Código:
raul338 es feo


Y con RegExp:
Código:
\[([^\[]+)\]

DoEvents! :P
« Última modificación: 17 Septiembre 2011, 16:53 pm por Psyke1 » En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
"""BUSCO EJEMPLO VB PARA ENVIAR MAILS""""
Programación Visual Basic
gera 1 6,257 Último mensaje 1 Septiembre 2005, 00:14 am
por programatrix
De donde puedo descargar utilidades: "Formas", "Estilos", "Motivos", D
Diseño Gráfico
Ad0nis 2 8,213 Último mensaje 2 Septiembre 2006, 15:48 pm
por Ad0nis
[Ayuda] modificar "start page" en "internet explorer" con "batch"
Scripting
taton 7 16,464 Último mensaje 20 Septiembre 2006, 01:45 am
por taton
Propiedad "Interprete"/"Artista"/"Autor" de una canción
Windows
Castg! 4 8,097 Último mensaje 16 Junio 2010, 07:58 am
por Roy-Mustang
Sistema>>Administracion>> ""No me aparece "Servicios""""
GNU/Linux
yoyoalee 4 8,984 Último mensaje 13 Febrero 2011, 18:34 pm
por leogtz
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines