Page 1 of 1

RegEx - Regular Expression, BackRef Replace / Extract

Posted: Wed Jul 20, 2011 2:06 pm
by graph100
Hi, I just have a need of using Regular expression, and I modify the procedure found HERE in order to have all the functionality I needed (Many many Thanks mdp !!)

The code allow functionality PHP like, in Replacing all the text reference,
and Extract all the reference needed in a list

In hope that will be usefull !

Code: Select all

;{ LIB code
ImportC ""
  pcre_exec(*pcre,*extra,subject.s,length,startoffset,options,*ovector,ovecsize)
EndImport


Structure List_Extraction
	Array Extract.s(0)
EndStructure


Procedure.s BackrefReplaceRegularExpression(regexp_handle, string.s, replacement.s, maximum_reference.l = 10) ; 0 <= maximum_reference <= 99
	Static Dim pcre_results(202)
	
	depart = 0
	
	While pcre_exec(PeekL(regexp_handle), 0, string, Len(string), depart, 0, @pcre_results(), 202) > 0
		; pcre_exec(PeekL(regexp_handle), 0, string, Len(string), pcre_results(1), 0, @pcre_results(), 202)

		rpl.s = replacement
		
		p = pcre_results(0)
		q = pcre_results(1)
		If FindString(replacement,"$0", 1)
			rpl = ReplaceString(rpl, "$0", PeekS(@string + p, q - p))
			
		EndIf
		
		tableau_ref = 2
		
		For _a = 1 To maximum_reference
			tag$ = "$" + Str(_a)
			
			If FindString(replacement, tag$, 1)
				p1 = pcre_results(tableau_ref)
				q1 = pcre_results(tableau_ref + 1)
				rpl = ReplaceString(rpl, tag$, PeekS(@string + p1, q1 - p1))
				
			EndIf
			
			tableau_ref + 2
		Next
		
		;       If FindString(replacement,"\2",1)
		;       	p1=pcre_results(4)
		;       	q1=pcre_results(5)
		;       	rpl=ReplaceString(rpl,"\2",PeekS(@string+p1,q1-p1))
		;       EndIf
		;       If FindString(replacement,"\3",1)
		;          p1=pcre_results(6)
		;          q1=pcre_results(7)
		;          rpl=ReplaceString(rpl,"\3",PeekS(@string+p1,q1-p1))
		;       EndIf
		
		depart = p + Len(rpl)
		
		string = Left(string, p) + rpl + Right(string, Len(string) - q)
	Wend
	
	ProcedureReturn string
EndProcedure

Procedure.s BackrefExtractRegularExpression(regexp_handle, string.s, Array Extract.b(1), List Resultat.List_Extraction()) ; 0 <= Taille tableau Extract <= 99
	Static Dim pcre_results(202)
	
	depart = 0
	
	maximum_reference = ArraySize(Extract())
	
	ClearList(Resultat())
	
	While pcre_exec(PeekL(regexp_handle), 0, string, Len(string), depart, 0, @pcre_results(), 202) > 0
		; pcre_exec(PeekL(regexp_handle), 0, string, Len(string), pcre_results(1), 0, @pcre_results(), 202)
		
		AddElement(Resultat())
		Dim Resultat()\Extract(maximum_reference)
		
		p = pcre_results(0)
		q = pcre_results(1)
		If Extract(0) = #True
			Resultat()\Extract(0) = PeekS(@string + p, q - p)
		EndIf
		
		tableau_ref = 2
		
		For _a = 1 To maximum_reference
			
			If Extract(_a) = #True
				p1 = pcre_results(tableau_ref)
				q1 = pcre_results(tableau_ref + 1)
				
				Resultat()\Extract(_a) = PeekS(@string + p1, q1 - p1)
			EndIf
			
			tableau_ref + 2
		Next
		
		depart = p ; + Len(rpl)
		
; 		string = Left(string, p) + rpl + Right(string, Len(string) - q)
		string = Left(string, p) + Right(string, Len(string) - q)
	Wend
	
	ProcedureReturn string
EndProcedure

;}

REGEX$ = " t(.*?)e(.*?) "

CHAINE$ = "As they functions tried importing them through the tset "

; regular expression creation
rh = CreateRegularExpression(0, REGEX$)


Debug "#### Recurrent Replacement test :"
Debug ""
Debug "Starting text :"
Debug CHAINE$
Debug "Regex = ||" + REGEX$ + "||"
Debug ""
Debug "Result :"
Debug BackrefReplaceRegularExpression(rh, CHAINE$, "________$0")


; creation of the array specifying which parenthesis must be returned
Dim tableau_extraction.b(2)

; we want all parenthesis so we complete the array all the way
For a = 0 To 2
	tableau_extraction(a) = #True
Next


; creation of the list returning the result
NewList Result.List_Extraction()


; processing
BackrefExtractRegularExpression(rh, CHAINE$, tableau_extraction(), Result())

Debug ""
Debug ""
Debug "#### Recurrent extraction test :"
Debug ""
Debug "Starting text :"
Debug CHAINE$
Debug "Regex = ||" + REGEX$ + "||"
Debug ""
Debug "Extracted values :"
Debug ""

b = 0
ForEach Result()
	Debug "Occurrence " + Str(b)
	
	For a = 0 To 2
		Debug "$" + Str(a) + " = " + Result()\Extract(a)
	Next
	
	b + 1
Next

Re: RegEx - Regular Expression, BackRef Replace / Extract

Posted: Sat Nov 12, 2011 3:47 pm
by Perkin
The pcre that gets imported is 32bit, to use on x64 you need to type a few of the variables to stay in 32bit range.
This adapted code works for both 32 an 64

Code: Select all

;{ LIB code
ImportC ""
	pcre_exec(*pcre,*extra,subject.s,length,startoffset,options,*ovector,ovecsize)
EndImport

Structure List_Extraction
	Array Extract.s(0)
EndStructure


Procedure.s BackrefReplaceRegularExpression(regexp_handle, string.s, replacement.s, maximum_reference.l = 10) ; 0 <= maximum_reference <= 99
	Static Dim pcre_results.l(202)
	
	depart = 0
	Repeat
		res.l = pcre_exec(PeekI(regexp_handle), 0, string, Len(string), depart, 0, @pcre_results(), 202)
		If res > 0
			
			rpl.s = replacement
			
			p = pcre_results(0)
			q = pcre_results(1)
			If FindString(replacement,"$0", 1)
				rpl = ReplaceString(rpl, "$0", PeekS(@string + p, q - p))
				
			EndIf
			
			tableau_ref = 2
			
			For _a = 1 To maximum_reference
				tag$ = "$" + Str(_a)
				
				If FindString(replacement, tag$, 1)
					p1 = pcre_results(tableau_ref)
					q1 = pcre_results(tableau_ref + 1)
					rpl = ReplaceString(rpl, tag$, PeekS(@string + p1, q1 - p1))
					
				EndIf
				
				tableau_ref + 2
			Next
			
			depart = p + Len(rpl)
			
			string = Left(string, p) + rpl + Right(string, Len(string) - q)
		EndIf
	Until res = -1
	
	ProcedureReturn string
EndProcedure

Procedure.s BackrefExtractRegularExpression(regexp_handle, string.s, Array Extract.b(1), List Resultat.List_Extraction()) ; 0 <= Taille tableau Extract <= 99
	Static Dim pcre_results.l(202)
	
	depart = 0
	
	maximum_reference = ArraySize(Extract())
	
	ClearList(Resultat())
	
	Repeat
		res.l = pcre_exec(PeekI(regexp_handle), 0, string, Len(string), depart, 0, @pcre_results(), 202)
		If res > 0
			; pcre_exec(PeekL(regexp_handle), 0, string, Len(string), pcre_results(1), 0, @pcre_results(), 202)
			
			AddElement(Resultat())
			Dim Resultat()\Extract(maximum_reference)
			
			p = pcre_results(0)
			q = pcre_results(1)
			If Extract(0) = #True
				Resultat()\Extract(0) = PeekS(@string + p, q - p)
			EndIf
			
			tableau_ref = 2
			
			For _a = 1 To maximum_reference
				
				If Extract(_a) = #True
					p1 = pcre_results(tableau_ref)
					q1 = pcre_results(tableau_ref + 1)
					
					Resultat()\Extract(_a) = PeekS(@string + p1, q1 - p1)
				EndIf
				
				tableau_ref + 2
			Next
			
			depart = p ; + Len(rpl)
			
			;	string = Left(string, p) + rpl + Right(string, Len(string) - q)
			string = Left(string, p) + Right(string, Len(string) - q)
		EndIf
	Until res =-1
	
	ProcedureReturn string
EndProcedure

;}

REGEX$ = " t(.*?)e(.*?) "

CHAINE$ = "As they functions tried importing them through the tset "

; regular expression creation
rh = CreateRegularExpression(0, REGEX$)


Debug "#### Recurrent Replacement test :"
Debug ""
Debug "Starting text :"
Debug CHAINE$
Debug "Regex = ||" + REGEX$ + "||"
Debug ""
Debug "Result :"
Debug BackrefReplaceRegularExpression(rh, CHAINE$, "________$0")


; creation of the array specifying which parenthesis must be returned
Dim tableau_extraction.b(2)

; we want all parenthesis so we complete the array all the way
For a = 0 To 2
	tableau_extraction(a) = #True
Next


; creation of the list returning the result
NewList Result.List_Extraction()


; processing
BackrefExtractRegularExpression(rh, CHAINE$, tableau_extraction(), Result())

Debug ""
Debug ""
Debug "#### Recurrent extraction test :"
Debug ""
Debug "Starting text :"
Debug CHAINE$
Debug "Regex = ||" + REGEX$ + "||"
Debug ""
Debug "Extracted values :"
Debug ""

b = 0
ForEach Result()
	Debug "Occurrence " + Str(b)
	
	For a = 0 To 2
		Debug "$" + Str(a) + " = " + Result()\Extract(a)
	Next
	
	b + 1
Next

Re: RegEx - Regular Expression, BackRef Replace / Extract

Posted: Thu Mar 08, 2012 3:57 pm
by graph100
sorry for my delay ;)
and many thanks