Between()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Between()

Post by dobro »

a function that an extract recovered between 2 tags, like this :)

Code: Select all


; between () permet de recuperer une chaine entre 2 autres


Procedure.s Between2(string.s, LString.s, RString.s)
	;/ Author : Dr. Dri ( bug fixed )
	Protected Between.s, lindex.l, RIndex.l
	
	lindex = FindString(string, LString, 0)
	RIndex = FindString(string, RString, lindex+Len(LString))
	
	If lindex And RIndex
		lindex  + Len(LString)
		Between = Mid(string, lindex, RIndex-lindex)
	EndIf
	
	ProcedureReturn Between
EndProcedure


MessageRequester("Between","Resultat à afficher : caratères entre Dr et py dans le mot Droopy"+Chr(10)+"Resultat : "+ Between2("Droopy", "Dr", "py"))
MessageRequester("Between2","Resultat à afficher : extraire l'url dans la source href="+Chr(34)+"http://www.purebasic.fr"+Chr(34) +Chr(10)+"Resultat : "+ Between2("href="+Chr(34)+"http://www.purebasic.fr"+Chr(34),"href="+Chr(34),Chr(34))); EPB
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Between()

Post by Danilo »

between() is better used for numbers IMO, like min() and max().

Code: Select all

Procedure between(var,num1,num2)
    If var >= num1 And var <= num2
        ProcedureReturn #True
    EndIf
EndProcedure

[...]

If between(Event,0,#WM_USER-1)
   msg$ = "SYSTEM MESSAGE: "           : Continue
ElseIf between(Event,#WM_USER,$7FFF)
   msg$ = "#WM_USER MESSAGE: "         : ;Continue
ElseIf between(Event,#WM_APP ,$BFFF)
   msg$ = "#WM_APP MESSAGE: "
ElseIf between(Event,$C000,$FFFF)
   msg$ = "STRING MESSAGE (RegisterWindowMessage): "
Else
   msg$ = "RESERVED BY SYSTEM MESSAGE: "
EndIf
You have another name for your string function? InStr()? InString()? ExtractString()?
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: Between()

Post by dobro »

the name really matter to me, the most important is the function :wink:
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
Joris
Addict
Addict
Posts: 890
Joined: Fri Oct 16, 2009 10:12 am
Location: BE

Re: Between()

Post by Joris »

Maybe ExtractRegularExpression() can be a bit of help ?
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Between()

Post by skywalk »

In the meantime, I use this...
It allows empty strings for the left and right and returns the 'Position After' the found search for further processing.

Code: Select all

Procedure.s SF_Between(SearchIn$, From$, sTo$, *PosAfter.Integer=0, StartPos.i=1)
  ; REV:  110519, skywalk
  ;       PB 4.6 FindString() added default StartPos and accepts <=0 entries without error.
  ; REV:  120409, skywalk
  ;       Allow From$ = "" to start from 1st character.
  ;       Allow sTo$  = "" to stop at last character.
  ; Return    = string between 2 multi-char delimiters
  ; *PosAfter = Text position after found String
  ; Syntax:
  ;    Debug SF_Between("<html>some text here</html>", "<html>", "</html>")
  ;    Debug SF_Between(r, "some", " ", @PosAfter, StartPos)
  Protected.i nLen1, nLen2, nLen, nLen3
  Protected.s Found$
  If From$
    nLen1 = FindString(SearchIn$, From$, StartPos)
  Else
    nLen1 = 1
  EndIf
  If nLen1
    If sTo$
      nLen2 = FindString(SearchIn$, sTo$, nLen1 + Len(From$))
    Else
      nLen2 = Len(SearchIn$) + 1
    EndIf
    If nLen2
      nLen = nLen1 + Len(From$)
      nLen3 = nLen2 - nLen
      Found$ = Mid(SearchIn$, nLen, nLen3)
      If (nLen + nLen3 > 0) And *PosAfter  ; Avoid Null Pointer error
        *PosAfter\i = nLen2 ;+ 1           ; used to be -> nLen
      EndIf
    EndIf
  EndIf
  ProcedureReturn Found$
EndProcedure
Define.i PosAfter
Define.s s$ = "<html>some text here</html>"
Debug Len(s$)
Debug SF_Between(s$, "<html>", "</html>", @PosAfter) + ", nextpos = " + Str(PosAfter)
Debug SF_Between(s$, "", "</html>", @PosAfter) + ", nextpos = " + Str(PosAfter)
Debug SF_Between(s$, "<html>", "", @PosAfter) + ", nextpos = " + Str(PosAfter)
Debug SF_Between(s$, "", "", @PosAfter) + ", nextpos = " + Str(PosAfter)
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: Between()

Post by dobro »

Thank you, though, I was not looking for a solution,

I suggested simply that'll be good, a function like this existed in PureBasic :)
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
Post Reply