Regular expression...

Just starting out? Need help? Post your questions and find answers here.
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Regular expression...

Post 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!
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Regular expression...

Post by PB »

Out of curiosity: why not just use FindString(text$,"time=",1) :?:
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
graves
Enthusiast
Enthusiast
Posts: 160
Joined: Wed Oct 03, 2007 2:38 pm
Location: To the deal with a pepper

Post 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," ")
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Post 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
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post 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.
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post 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. :wink:
--Kale

Image
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post 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)
--Kale

Image
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Post 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

Please correct my english
http://purebasic.developpez.com/
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Post 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)
superadnim
Enthusiast
Enthusiast
Posts: 480
Joined: Thu Jul 27, 2006 4:06 am

Post by superadnim »

Question is... which ones faster?
Bonne_den_kule
Addict
Addict
Posts: 841
Joined: Mon Jun 07, 2004 7:10 pm

Post by Bonne_den_kule »

Is grouping and backreferences supported by PB's regex library?
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Post by Hroudtwolf »

"Grouping" yes.
But "Backreferences"?
Do you mean, something like recycling of expressions?
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Post 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
Please correct my english
http://purebasic.developpez.com/
User avatar
hallodri
Enthusiast
Enthusiast
Posts: 208
Joined: Tue Nov 08, 2005 7:59 am
Location: Germany
Contact:

Post 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)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

All these long code examples. :roll: Again: what's wrong with FindString?
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Post Reply