Page 1 of 1

[SOLVED] Regular Expression ( pcre_exec ) - BackReferences

Posted: Thu Mar 21, 2013 10:13 am
by eddy
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)

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")

Re: Regular Expression ( pcre_exec ) : BackReferences not fo

Posted: Thu Mar 21, 2013 4:00 pm
by Perkin
It may be because PCRE is 32bit, check this post, and you should be able to see what needs adjusting in your code.

The code there is adapted BackrefReplaceRegularExpression procedure, so you should be able to just replace existing.

Re: Regular Expression ( pcre_exec ) : BackReferences not fo

Posted: Thu Mar 21, 2013 4:52 pm
by eddy
Perkin wrote:It may be because PCRE is 32bit, check this post, and you should be able to see what needs adjusting in your code.

The code there is adapted BackrefReplaceRegularExpression procedure, so you should be able to just replace existing.
I see
Thx