Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Including "End of String" in character class?

by doran (Deacon)
on Mar 16, 2007 at 19:01 UTC ( [id://605197]=perlquestion: print w/replies, xml ) Need Help??

doran has asked for the wisdom of the Perl Monks concerning the following question:

I'm parsing an mp3 stream to pull out the title of the song. To do this, I have this regex:

$line =~ m/StreamTitle='(.*?)';/i;

This is fine if the line is something like this:

{<F-#f]?o!StreamTitle='Five Keys - Girl You Better Stop It';wFU1*

For reasons I'm not sure of, sometimes the line is truncated and doesn't have the closing ';, so I need regex which says grab everything after StreamTitle=' up until '; or the end of line, whichever comes first. It's as if I need to include the end of string in a character class, but doing something like [;\Z] doesn't work. I'm sure there must be a way, but don't know how.

Thanks in advance.

Replies are listed 'Best First'.
Re: Including "End of String" in character class?
by Fletch (Bishop) on Mar 16, 2007 at 19:05 UTC

    Perhaps /StreamTitle='(.*?)(?=';|\z)/ might work?

      Works like a charm. Thanks!
Re: Including "End of String" in character class?
by ikegami (Patriarch) on Mar 16, 2007 at 19:16 UTC

    If you extend what you already have,
    $line =~ m/StreamTitle='(.*?)(?:'|\z)/i;
    or just
    $line =~ m/StreamTitle='(.*?)'?/i;

    However, if you replace the .*? with the more predictable [^']*, you don't even have to match the end quote:
    $line =~ m/StreamTitle='([^']*)/i;

    Update: I knew I avoided .*? for a reason. Struck out a bad "solution".

      Thanks ikegami. Indeed, after some testing, that last one seems to be best.
Re: Including "End of String" in character class?
by japhy (Canon) on Mar 17, 2007 at 02:11 UTC
    Just a friendly reminder... if it ain't a character, it don't go in a character class.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: Including "End of String" in character class?
by shigetsu (Hermit) on Mar 16, 2007 at 19:19 UTC
    Assuming you'd save the stream to a file, you could use MP3::M3U to parse the absolute paths of each song and hand them over to MP3::Tag.

    (just for the records)
      Unfortunately MP3::M3U is for playlists, and I'm parsing a stream, so there's no file. Thanks though.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://605197]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-16 14:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found