Título: Extraer string entre "[" y "]"
Publicado por: extreme69 en 17 Septiembre 2011, 01:35 am
Tengo: 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
Public Function StrBetween(Cadena As String, SubCadena1 As String, Subcadena2 As String) As String Dim Pos1 As Integer, Pos2 As Integer Pos1 = InStr(UCase(Cadena), UCase(SubCadena1)) + 1 Pos2 = InStr(UCase(Cadena), UCase(Subcadena2)) If Pos1 <> 0 And Pos2 <> 0 Then StrBetween = Mid$(Cadena, Pos1, Pos2 - Pos1) End If End Function Private Sub Form_Load() Dim ASd As String Dim Asd2 As String ASd = "aitheoiethi[BLABLABLAkjhuihui]taihoithaoihtoea" Asd2 = StrBetween(ASd, "[", "]") MsgBox Asd2 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 '------------------------------------------------------------------------ ' *Function : Text_Between_Words ' *Author : *PsYkE1* ' *Mail : vbpsyke1@mixmail.com ' *Date : 10/4/10 ' *Purpose : It returns the text wich is between two words ' *Recommended Websites : ' http://foro.rthacker.net/ ' http://InfrAngeluX.Sytes.Net/ '------------------------------------------------------------------------ Option Explicit Public Function Text_Between_Words(ByVal sTextToAnalyze As String, ByVal sStartWord As String, ByVal sEndWord As String) As String Dim iPosition1 As Integer Dim iPosition2 As Integer Dim iStart As Integer iPosition1 = InStr(sTextToAnalyze, sStartWord) If CBool(iPosition1) Then iStart = iPosition1 + Len(sStartWord) iPosition2 = InStr(iStart, sTextToAnalyze, sEndWord) If CBool(iPosition2) Then Text_Between_Words = Mid$(sTextToAnalyze, iStart, iPosition2 - iStart) End If End If End Function
Debug.Print Text_Between_Words("El contexto es el ámbito de referencia de un texto. ¿Qué entiendo por ámbito de referencia?.", "referencia", "referencia")
Dulces Lunas!¡.
Título: Re: Extraer string entre "[" y "]"
Publicado por: seba123neo en 17 Septiembre 2011, 06:14 am
Private Sub Form_Load() MsgBox TextoEntreMedio("aitheoiethi[BLABLABLA]taihoithaoihtoea", "[", "]") End Sub Private Function TextoEntreMedio(Texto As String, Palabra1 As String, Palabra2 As String) TextoEntreMedio = Left$(Mid$(Texto, InStr(Texto, Palabra1) + Len(Palabra1)), InStr(Mid$(Texto, InStr(Texto, Palabra1) + Len(Palabra1)), Palabra2) - 1) End Function
Título: Re: Extraer string entre "[" y "]"
Publicado por: BlackZeroX en 17 Septiembre 2011, 06:49 am
Private Sub Form_Load() MsgBox TextoEntreMedio("aitheoiethi[BLABLABLA]taihoithaoihtoea", "(", "]") End Sub
Dulces Lunas!¡.
Título: Re: Extraer string entre "[" y "]"
Publicado por: Psyke1 en 17 Septiembre 2011, 16:30 pm
Un poco mejorada: Option Explicit Public Static Function TextBetweenWords$(ByRef sText$, ByVal sWord1$, ByVal sWord2$) Dim lPos1&, lPos2&, lStart& If LenB(sText) Then lPos1 = InStrB(1, sText, sWord1, vbBinaryCompare) If lPos1 Then lStart = lPos1 + LenB(sWord1) lPos2 = InStrB(lStart, sText, sWord2, vbBinaryCompare) If lPos2 Then TextBetweenWords = MidB$(sText, lStart, lPos2 - lStart) End If End If End If End Function Private Sub Form_Load() Debug.Print TextBetweenWords("qwertysdfcv [raul338 es feo] prueba", "[", "]") End Sub
Devuelve:
Y con RegExp: DoEvents! :P
|