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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_agx_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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aescribensAuthor Commented:
Thank You!
0
_agx_Commented:
You're welcome :)
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ColdFusion Language

From novice to tech pro — start learning today.