http://qs321.pair.com?node_id=342771


in reply to some regex help

First of all, creating variables like that is quite dangerous. You could quite easily find yourself setting a value that might overwrite something in your program. Having a hash that has the variable name as it's key, and the variable's value as it's value is much safer.

Second, the /%(.*)\s*=\s*(.*)/ regexp is not exactly the same as the other one: the .* is greedy, therefore it will consume all the blanks before the equals sign. Your $1 will have trailing blanks if there are any trailing blanks to be had.(And what is the % doing there?)

Third, you do know that the $options as you posted it here will not match MN, don't you>? This is because it doesn't contain the letter M or N, and because it only accepts one letter, you need a + or a count like {n,m} to match it correctly.

Replies are listed 'Best First'.
Re: Re: some regex help
by emilford (Friar) on Apr 05, 2004 at 21:57 UTC
    Yes, that makes sense. I'll use a hash to store the dynamic variables. Duh, I should have thought of that. :)

    The % sign in my second regex was meant to be a ^ sign. Typo.

    I think I need to rethink the $options part. Say the $options variable was set to "A|B|C". In the configuration file that I need to parse, the line could be "Foo = A" or "Bar = A|B" to signify it either A or B. I don't think what I have will give me the desired results.

    I figured using (.*) would be bad. You know, because of the whole greedy thing....:-P. Thanks.