Foro de elhacker.net

Programación => Programación Visual Basic => Mensaje iniciado por: extreme69 en 17 Septiembre 2011, 01:35 am



Título: Extraer string entre "[" y "]"
Publicado por: extreme69 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?





Título: Re: Extraer string entre "[" y "]"
Publicado por: DarkMatrix 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...


Título: Re: Extraer string entre "[" y "]"
Publicado por: extreme69 en 17 Septiembre 2011, 01:56 am
Excelente, muchas gracias.  ;-)



Título: Re: Extraer string entre "[" y "]"
Publicado por: BlackZeroX 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!¡.


Título: Re: Extraer string entre "[" y "]"
Publicado por: seba123neo 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


Título: Re: Extraer string entre "[" y "]"
Publicado por: BlackZeroX 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!¡.


Título: Re: Extraer string entre "[" y "]"
Publicado por: Psyke1 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