Page 1 of 2
Regular expression...
Posted: Fri Feb 01, 2008 1:30 pm
by Inf0Byt3
Can anyone give me a kick-start and help me extract the time value from the following line with regular expressions?
Code: Select all
frame= 152 fps= 76 q=0.0 size= 766kB time=14.8 bitrate= 423.8kbits/s
I need to extract "14.8" from there but I can't find any way to do it... Any help appreciated. Thanks!
Re: Regular expression...
Posted: Fri Feb 01, 2008 1:38 pm
by PB
Out of curiosity: why not just use FindString(text$,"time=",1)

Posted: Fri Feb 01, 2008 1:42 pm
by graves
frame= 152 fps= 76 q=0.0 size= 766kB time=14.8 bitrate= 423.8kbits/s
Code: Select all
time.s = StringField(StringField(data,6,"="),1," ")
Posted: Fri Feb 01, 2008 2:06 pm
by Hroudtwolf
A little funny overhead....
Code: Select all
Structure tCHARACTER
StructureUnion
c.c
s.s {1}
EndStructureUnion
EndStructure
Define.s sText = "frame= 152 fps= 76 q=0.0 size= 766kB time=14.8 bitrate= 423.8kbits/s"
Define.tCHARACTER *Source = @sText
Define.l lLenSW = Len ("time=")
Define.s sTemp = ""
While *Source\c
If PeekS (*Source , lLenSW * SizeOf (tCHARACTER)) = "time="
*Source + (SizeOf (tCHARACTER) * lLenSW)
While *Source\c And *Source\c <> 32
sTemp + *Source\s
*Source + SizeOf (tCHARACTER)
Wend
Debug "My searched number: " + sTemp
Break
EndIf
*Source + SizeOf (tCHARACTER)
Wend
Best regards
Wolf
Posted: Fri Feb 01, 2008 2:06 pm
by Inf0Byt3
Oh, well I can't use stringfield anymore since with the new code lines may get joined, so stringfield would return another field. I fixed it now with this:
Code: Select all
TimeStr.s = Mid(Err,FindString(Err,"Duration: ",0)+Len("Duration: "),10)
Thought RegExp would be a little bit more solid

.
Thanks for the quick help!
[Edit]
Sorry, slow typing...
Nice code Hroudtwolf... I think yours instead of the Mid one. Thanks.
Posted: Fri Feb 01, 2008 7:45 pm
by Kale
The new beta Regular Expression lib doesn't seem to support tagged expressions but you can do it like this:
Code: Select all
Dim Results.s(10)
Text.s = "frame= 152 fps= 76 q=0.0 size= 766kB time=14.8 bitrate= 423.8kbits/s"
CreateRegularExpression(1, "[0-9]*\.?[0-9*]")
Count.l = ExtractRegularExpression(1, Text, Results())
For x = 0 To Count - 1
Debug Results(x);
Next
The result your after is the 5th array index.

Posted: Fri Feb 01, 2008 7:53 pm
by Kale
This one is a little more robust in that it doesn't find all the numbers in the string just the one your after:
Code: Select all
Dim Result.s(0)
Text.s = "frame= 152 fps= 76 q=0.0 size= 766kB time=14.8 bitrate= 423.8kbits/s"
;extract the time section
CreateRegularExpression(1, "time=\s*[0-9]*\.?[0-9*]")
Count.l = ExtractRegularExpression(1, Text, Result())
;extract the number from the time section
CreateRegularExpression(1, "[0-9]*\.?[0-9*]")
Count.l = ExtractRegularExpression(1, Result(0), Result())
Debug Result(0)
Posted: Fri Feb 01, 2008 7:57 pm
by Comtois
while waiting someone show a code more simple
Code: Select all
Dim Results.s(0)
Dim Values.s(0)
Text.s = "frame=152fps=76q=0.0size=766kBtime=14.8bitrate=423.8kbits/s"
CreateRegularExpression(1, "time=[0-9]*\.?[0-9*]")
CreateRegularExpression(2, "[0-9]*\.?[0-9*]")
Count.l = ExtractRegularExpression(1, Text, Results())
For x = 0 To Count - 1
nb=ExtractRegularExpression(2, Results(x), Values())
For i=0 To nb-1
Debug Values(i)
Next i
Next
Posted: Fri Feb 01, 2008 8:52 pm
by Hroudtwolf
Another way....
Code: Select all
sText.s = "frame=152fps=76q=0.0size=766kBtime=14.8bitrate=423.8kbits/s"
Debug StrF(ValF (Right (sText , Len (sText) - FindString (sText , "time=" , 1) - 4)) , 2)
Posted: Sun Feb 03, 2008 7:26 pm
by superadnim
Question is... which ones faster?
Posted: Sun Feb 03, 2008 7:36 pm
by Bonne_den_kule
Is grouping and backreferences supported by PB's regex library?
Posted: Sun Feb 03, 2008 8:21 pm
by Hroudtwolf
"Grouping" yes.
But "Backreferences"?
Do you mean, something like recycling of expressions?
Posted: Sun Feb 03, 2008 8:38 pm
by Comtois
superadnim wrote:Question is... which ones faster?
I dont know, but with this one you keep decimal
Code: Select all
Dim Results.s(0)
Dim Values.s(0)
Text.s = "frame=152fps=76q=0.0size=766kBtime=14.87612345bitrate=423.8kbits/s"
CreateRegularExpression(1, "time=[0-9]*\.?[0-9*]++")
CreateRegularExpression(2, "[0-9]*\.?[0-9*]++")
Count.l = ExtractRegularExpression(1, Text, Results())
For x = 0 To Count - 1
nb=ExtractRegularExpression(2, Results(x), Values())
For i=0 To nb-1
Debug Values(i)
Next i
Next
Posted: Sun Feb 03, 2008 8:41 pm
by hallodri
Bonne_den_kule wrote:Is grouping and backreferences supported by PB's regex library?
Not realy. But you can try this :
Code: Select all
ImportC ""
pcre_exec(regex,*extra,subject.s,len,offset,options,*ovector,ovecsize)
pcre_get_named_substring(regex,subject.s,*ovector,stringcount,stringname.s,*stringptr.string)
EndImport
Procedure.s ExtractSubName(id,subject.s,name.s)
Protected result.s
Protected Dim Ovec(100)
Protected String.l
Protected stringcount.l
Protected *regex.long = IsRegularExpression(id)
If *regex
stringcount = pcre_exec(*regex\l,0,subject,Len(subject),0,0,@ovec(),100)
If stringcount > 0
If pcre_get_named_substring(*regex\l,subject,@ovec(),stringcount,name,@string) > 0
result = PeekS(String)
EndIf
EndIf
EndIf
ProcedureReturn result
EndProcedure
Text.s = "frame=152fps=76q=0.0size=766kBtime=14.8bitrate=423.8kbits/s"
CreateRegularExpression(0, "time=(?P<TIME>[0-9]*\.?[0-9*])")
Debug ExtractSubName(0,Text,"TIME")
or
Code: Select all
ImportC ""
pcre_get_substring(subject.s,*ovector,stringcount,stringnumber,*stringptr)
pcre_exec(regex,*extra,subject.s,len,offset,options,*ovector,ovecsize)
EndImport
Procedure.s ExtractSubID(id,subject.s,subid)
Protected result.s
Protected Dim Ovec(100)
Protected String.l
Protected stringcount.l
Protected *regex.long = IsRegularExpression(id)
If *regex
stringcount = pcre_exec(*regex\l,0,subject,Len(subject),0,0,@ovec(),100)
If stringcount > 0
If pcre_get_substring(subject,@ovec(),stringcount,subid,@string) > 0
result = PeekS(String)
EndIf
EndIf
EndIf
ProcedureReturn result
EndProcedure
Text.s = "frame=152fps=76q=0.0size=766kBtime=14.8bitrate=423.8kbits/s"
CreateRegularExpression(0, "time=([0-9]*\.?[0-9*])")
Debug ExtractSubID(id,Text,1)
Posted: Sun Feb 03, 2008 8:48 pm
by PB
All these long code examples. :roll: Again: what's wrong with FindString?