[SOLVED] Regular Expression ( pcre_exec ) - BackReferences
Posted: Thu Mar 21, 2013 10:13 am
Hi,
I found a PB4 code to replace regular expression with bakcreferences
But I didn't manage to run it correctly on PB 5.1 x64
The function find no backreferences. (code fixed)
I found a PB4 code to replace regular expression with bakcreferences
But I didn't manage to run it correctly on PB 5.1 x64
The function find no backreferences. (code fixed)
Code: Select all
ImportC ""
CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
pcre_exec.l(*pcre, *extra, subject.s, length, startoffset, options, *ovector, ovecsize) As "pb_pcre_exec"
CompilerElse
pcre_exec.l(*pcre, *extra, subject.s, length, startoffset, options, *ovector, ovecsize) As "_pb_pcre_exec"
CompilerEndIf
EndImport
Procedure.s BackrefReplaceRegularExpression(RegularExpression, string.s, replacement.s, maximumReference.l = 10) ; 0 <= maximum_reference <= 99
Static Dim pcre_results.l(100,1)
Protected start = 0
Repeat
Protected res.l = pcre_exec(PeekI(RegularExpression), 0, string, Len(string), start, 0, @pcre_results(), 202)
If res=-1 : Break : EndIf
Protected rpl.s = replacement
Protected p = pcre_results(0,0)
Protected q = pcre_results(0,1)
If FindString(replacement,"\0", 1)
rpl = ReplaceString(rpl, "\0", PeekS(@string + p, q-p))
EndIf
For _a = 1 To maximumReference
tag$ = "\" + Str(_a)
If FindString(replacement, tag$, 1)
Protected p1 = pcre_results(_a,0)
Protected q1 = pcre_results(_a,1)
rpl = ReplaceString(rpl, tag$, PeekS(@string + p1, q1-p1))
EndIf
Next
start = p + Len(rpl)
string = Left(string, p) + rpl + Right(string, Len(string)-q)
ForEver
ProcedureReturn string
EndProcedure
Regex = CreateRegularExpression(0, "(.*?)ure(.*?)asic")
Debug BackrefReplaceRegularExpression(Regex, "PureBasic", "\1\2")