Extracting Value from String

Hello Experts,

How can I extract certain values from a string?

For example, I have the following string...

version: 51.9.0 built on Feb 12 2007 12:01:35, gcc: 3.4.5 (mingw special) Seems stream 0 codec frame rate differs from container frame rate: 1000.00 (1000/1) -> 25.00 (25/1) Input #0, flv, from 'c:\videos\testvideo.flv': Duration: 00:04:19.1, start: 0.000000, bitrate: 64 kb/s Stream #0.0: Video: flv, yuv420p, 320x240, 25.00 fps(r) Stream #0.1: Audio: mp3, 22050 Hz, mono, 64 kb/s

Now, I need to extract the following values...

flv (which is always next to Video: )
00:04:19.1 (which is always next to Duration: )

I tried the following:

<cfset duration_ = #listgetat(result,55," ")#>
<cfif right(duration_, 1) is ",">
    <cfset duration_ = left(duration_, len(duration_)-1)>
</cfif>

But the problem is that the order of the word count changes constantly and I get different results, so I think the safest way to do this is to target the values next to the keywords (Duration, Video).

Thanks!
Anthony
LVL 1
aescribensAsked:
Who is Participating?
 
_agx_Connect With a Mentor Commented:
If the text is dynamic, a regex is the best approach.  There's probably a more elegant method, but here's one example.


<cfsavecontent variable="str">
version: 51.9.0 built on Feb 12 2007 12:01:35, gcc: 3.4.5 (mingw special) 
Seems stream 0 codec frame rate differs from container frame rate: 1000.00 (1000/1) -> 25.00 (25/1) 
Input #0, flv, from 'c:\videos\testvideo.flv': Duration: 00:04:19.1, start: 0.000000, bitrate: 64 kb/s Stream #0.0: Video: flv, yuv420p, 320x240, 25.00 fps(r) Stream #0.1: Audio: mp3, 22050 Hz, mono, 64 kb/s
</cfsavecontent>

   <!--- initialize variables --->
   <cfset duration = "">
   <cfset type    = "">

   <cfset result = reFind("Duration: ([^,])+", str, 1, true)>
   <cfif arrayLen(result.pos)>
      <!--- If duration pattern was found, extract value ...--->
      <cfset duration = listRest(mid(str, result.pos[1], result.len[1]), ":")>
   </cfif>
   
   <cfset result = reFind("Video: [^,]+", str, 1, true)>
   <cfif arrayLen(result.pos)>
      <!--- If type pattern was found, extract value ...--->
      <cfset type = listRest(mid(str, result.pos[1], result.len[1]), ":")>
   </cfif>

   <!--- results --->
   <cfoutput>
      duration=#duration#<br>
      type=#type#<br>
   </cfoutput>

Open in new window

0
 
aescribensAuthor Commented:
Thank You!
0
 
_agx_Commented:
You're welcome :)
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
aescribensAuthor Commented:
agx:  I have a follow-up question...  What if I need to extract only the Email portion from the following string:  John <jdoe@gmail.com>

How would I do that?
0
 
_agx_Commented:
Hi @aescribens - I see you got an answer already here http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/Cold_Fusion_Markup_Language/Q_26282914.html?cid=1576

BTW: It's usually better to open a new question because many experts stop monitoring questions after they're answered. I just happened to still be monitoring this one :)
0
 
aescribensAuthor Commented:
Yes I noticed.  :)  Thanks.
0
All Courses

From novice to tech pro — start learning today.