Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: In need of a stupid regex trick

by David Caughell (Monk)
on Jan 04, 2004 at 22:38 UTC ( [id://318714]=note: print w/replies, xml ) Need Help??


in reply to In need of a stupid regex trick

My instincts were that it can be done, since regex's are where perl really shines.

I'm still learning the language, so I've decided to give this a shot just for fun. Jweed's elegant solution (and whoever put it out on cpan) is the best if you're doing this sort of thing for any other purpose than learning the language, though.

Now on to something not quite so elegant:

#!/usr/bin/perl -w use strict; my $string = 'one "two three" four five "six seven eight" nine'; my @list = split / [ ]" #opening quotes | #or "[ ] #closing quotes | [ ] #a space (?!.*?\w") # that's not before any number of characters followed # by a closing quote (allows EOL at quote) /x, $string;

OOC, is it possible to put a character group (and quantify it with a * + ? or {} ) into a look-ahead match?

Crap, this isn't quite there yet. The four and five are sticking together. If anyone has suggestions on how to fix that, I'd appreciate it.


$scratchpad_public = 0 unless $scratchpad;

Replies are listed 'Best First'.
Re: Re: In need of a stupid regex trick
by bart (Canon) on Jan 05, 2004 at 01:30 UTC
    is it possible to put a character group (and quantify it with a * + ? or {} ) into a look-ahead match?
    Sure it is. Anything you can do in a plain match, you can do in a lookahead match.

    For lookbehind, it's a different matter: you can only use fixed length lookbehind, so quantifiers (like * and +), and varied-length alternatives, are out. BTW if all alternatives have the same length, it is allowed, as in:

    $_ = q[There's food at the bar.]; while(/(?<=foo|bar)(\S+)/g) { print "$1\n"; }
Re: Re: In need of a stupid regex trick
by jweed (Chaplain) on Jan 04, 2004 at 23:42 UTC
    Also, you need to be nice to embedded strings such as "two three ". Currently, your regex calls the second quotation mark an "opening quote" and throws it away. If these spaces are crucial, then that's no good.


    Who is Kayser Söze?
    Code is (almost) always untested.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 14:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found