Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Doesn't seem complete

by tlhf (Scribe)
on Jun 29, 2002 at 16:21 UTC ( [id://178247]=note: print w/replies, xml ) Need Help??


in reply to Re: Regular expression double grouping negation headache
in thread Regular expression double grouping negation headache

Okay, so we've got;

%defaults = map {(/(.*?)=(.*)/)} @ARGV;
Which is cute. Except it fails under some conditions. Let's say you want to give the variable 'foo' the value of 'moo shoo= coo'. I'd assume it would be written 'foo=moo shoo\= coo'. Except your example doesn't allow for this.

So I tried a little, and this is what I got;

I was working from the param line as a single scalar variable for simplicity, and my first efforts seemed moderately successful;

$line = 'foo=moo sho=clue\ woo\=moo'; @pairs = split(/(?<!\\)\s/, $line);


However, this system has the problem of only checking whether the previous character is a backslash - it doesn't allow for backslashed backslahes. The expression would need to understand that '\\=' signified a backslash and then a non-escape literal. But it couldn't just check for two slashes and cancel, for it should accept '\\\=' as a a backslashed backslash and a backslashed equals symbol. It would, effectively, need to look behind for an even number of slashes, and only slash on that.

I had;
@pairs = split(/(?<!(\\\\+))\s/, $line); Which seemed perfect. Except we're not allowed variable length lookbehind, because it's not been implemented yet. Which was very annoying to find out. So, we could match the proceeding backslashes normally, but strap them back on. But that would be horrible. I have tried further routes, but found nothing. *sigh*

tlhf
xxx

Replies are listed 'Best First'.
Re: Doesn't seem complete
by dreadpiratepeter (Priest) on Jun 29, 2002 at 22:21 UTC
    No, it would be written foo=moo\ shoo=coo and will be parsed right by my code. The big limitation is that you can't have an = in the key part but I can't see that being much of a problem. And actually an assertion would handle that case.

    -pete
    "Pain heals. Chicks dig scars. Glory lasts forever."
      Er, the limitation of not being able to parse an equals sign was the one I was talking about it.

      Which is cute. Except it fails under some conditions. Let's say you want to give the variable 'foo' the value of 'moo shoo= coo'. I'd assume it would be written 'foo=moo shoo\= coo'. Except your example doesn't allow for this.
      Your code wouldn't parse it correctly, although an assertion would be emminently acceptable.

      tlhf
      xxx
        Once again, you have misunderstood the problem. The string you are passing in will be parsed by the shell as 2 arguments. The first being foo=moo which will properly be parsed as foo => moo. The second being shoo\= coo which will also be properly parsed as shoo\ => coo. In order to embed a space into the tag you need to escape the space, so that the shell will parse it as one argument. Thus making the correct string, foo=moo\ shoo=\ coo
        Try the code I posted on the strings and you will see that it works. the limitation is that it can't parse an equal sign in the key, not in the value as you are trying to do.



        -pete
        "Pain heals. Chicks dig scars. Glory lasts forever."
Re: Doesn't seem complete
by Aristotle (Chancellor) on Jun 29, 2002 at 22:20 UTC
    Let's say you want to give the variable 'foo' the value of 'moo shoo= coo'. I'd assume it would be written 'foo=moo shoo\= coo'.

    I would think that would be foo=moo\ shoo=coo in a Unixish shell and "foo=moo shoo=coo" using an MSish one. In either case /(.*?)=(.*)/ does the job just fine since it will unconditionally use the first equals sign as the delimiter of the variable name, and then just slurp the rest of the string as the value, equals signs or not.

    An inadquacy does therefor arise when one wants an equals sign in the variable name, but I don't know if allowing for that special case is even desired, and even if so whether it's worth going through the tremendous pain of handling escapes properly (a problem if I've repeatedly broken my teeth on - it's not possible without a true, if simple, parser).

    Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-23 19:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found