Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Simple re question

by DarknessX (Scribe)
on Jan 27, 2003 at 20:55 UTC ( #230354=note: print w/replies, xml ) Need Help??

in reply to Simple re question

I've rewritten the regex with your help, thank you much. As for the missing tilde, that was just tired idiocy. My next question again deals with special characters in general.

Why is there such a performance penalty for using them? Is it appreciable at all in a script which is only 5 or so lines (If you didn't figure it out, the above was just to get the songname of whatever's in XMMS, using xwindow -root -all | grep XMMS_Window.)? Without doing it stupidly (like mine above) or with ubercomplex regex (semi- is fine, I'm still learning) is there a way to get "from the beginning until char"? Muchos Gracias.

Replies are listed 'Best First'.
Re^2: Simple re question
by diotalevi (Canon) on Jan 27, 2003 at 21:17 UTC

    It's not such a big deal. You're forcing the regex engine to make an extra copy of your data which in this case is a few measly bytes. It can make a difference for other scripts but you shouldn't even worry about it here. You'd need an advanced degree in Regex-ology to know exactly why though. I'd have to refer back to my copy of Mastering Regular Expressions to get the exact semantics.

    The general idea though is that those special variables must still be valid even if you alter or mangle the original string. The only way that works is if perl makes a new copy just for them. So that's the overhead. In this case it just doesn't matter.

    If your data is normal enough then you can use something like this $title =~ m{(<=- )([^"]+)}; on it. The return value (true/false) indicates whether it matched or not and the title is stored in $1.

    Seeking Green geeks in Minnesota

Re: Re: Simple re question
by Enlil (Parson) on Jan 27, 2003 at 22:20 UTC
    Without doing it stupidly (like mine above) or with ubercomplex regex (semi- is fine, I'm still learning) is there a way to get "from the beginning until char"?

    If you want to capture up to a certain character you can just capture it with parens and [^-]. The ^ right after the [ makes the match anything besides whatever is in this character class (ie. anything within []). There is no need for lookahead or lookbehind negative width assertions. That is:

    will capture anything besides a "-" as many times as it can,and store that in $1, if you want it from the beginning you can add a ^ to the start of the regex (note the different meaning of ^ outside the character class square brackets), like so
    This will anchor your match to start from the beginning. So any line starting with "-" will fail.


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://230354]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2021-11-29 18:52 GMT
Find Nodes?
    Voting Booth?

    No recent polls found