[SOLVED] Regular Expression ( pcre_exec ) - BackReferences

Just starting out? Need help? Post your questions and find answers here.
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

[SOLVED] Regular Expression ( pcre_exec ) - BackReferences

Post 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")
Last edited by eddy on Thu Mar 21, 2013 5:15 pm, edited 4 times in total.
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
Perkin
Enthusiast
Enthusiast
Posts: 504
Joined: Thu Jul 03, 2008 10:13 pm
Location: Kent, UK

Re: Regular Expression ( pcre_exec ) : BackReferences not fo

Post 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.
%101010 = $2A = 42
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Re: Regular Expression ( pcre_exec ) : BackReferences not fo

Post 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
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
Post Reply