Hallo,
I had the same question
http://www.purebasic.fr/german/viewtopi ... =3&t=25124.
In the above thread edel shows me how to get the group result. I put it into a procedure to handle it like ExtractRegularExpression does.
The procedure put the full solution and the content of the groups into an structured array. Each array element is containing only a list. The first element of every list gets the whole solution and every next element is containing the content of the groups.
Here is the code:
Code: Select all
ImportC ""
pb_pcre_exec(*pcre, *extra, subject.p-ascii, length, startoffset, options, *ovector, ovecsize)
pb_pcre_get_substring(subject.p-ascii, *ovector, stringcount, stringnumber, *stringptr)
pb_pcre_free_substring(*stringptr)
EndImport
Structure RegEx_Klammern
List RegEx_SubItem.s()
EndStructure
EnableExplicit
Procedure ExtractRegExItems(Regex,subject.s,Array Items.RegEx_Klammern(1))
Protected len = Len(subject), offset = 0, first_sub = 0, count = 0
Protected TrefferCounter = -1, Erg = 0, numcount = 0, Text$ = ""
Protected Dim ovec(30)
If regex
count = pb_pcre_exec(PeekL(regex), 0, subject, len, offset, 0, @ovec(), 30)
While count>0
TrefferCounter +1
ReDim Items(TrefferCounter)
Erg = 0
For numCount = 0 To count-1
Erg= pb_pcre_get_substring(subject, ovec(), count, numcount, @first_sub)
If Erg >= 0
Text$ = PeekS(first_sub,-1,#PB_Ascii)
AddElement(Items(TrefferCounter)\RegEx_SubItem())
Items(TrefferCounter)\RegEx_SubItem() = Text$
EndIf
Next
pb_pcre_free_substring(first_sub)
If Offset = ovec(1)
offset = ovec(1)+1
Else
offset = ovec(1)
EndIf
count = pb_pcre_exec(PeekL(regex), 0, subject, len, offset, 0, @ovec(), 30)
Wend
EndIf
ProcedureReturn TrefferCounter +1
EndProcedure
; DEMO
Dim Items.RegEx_Klammern(0)
Define subject.s = "abc123abc def456def"
Define pattern.s = "([a-z]+)([0-9]+)\1"
Define RegEx, Groesse, i, Text$, Counter
RegEx = CreateRegularExpression(#PB_Any, pattern)
Groesse = ExtractRegExItems (RegEx,subject, Items())
For i = 0 To Groesse-1
Counter = 0
ForEach Items(i)\RegEx_SubItem()
If Counter = 0
Text$ = "full solution: "
Else
Text$ = "Content of "+Str(Counter)+". group: "
EndIf
Debug Text$ + Items(i)\RegEx_SubItem()
counter +1
Next
Debug "--------------------------------"
Next
NicknameFJ