Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: Splitting on escapable delimiter

by Roy Johnson (Monsignor)
on Mar 28, 2008 at 18:24 UTC ( [id://677081]=note: print w/replies, xml ) Need Help??


in reply to Re: Splitting on escapable delimiter
in thread Splitting on escapable delimiter

If you consume the separator, you don't have to filter it out. And if you put the escape regex first, you don't have to mention the # twice.
my @fields = /((?:#.|[^@])*)\@?/sg;

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^3: Splitting on escapable delimiter
by ikegami (Patriarch) on Mar 28, 2008 at 21:10 UTC
    Nope, that returns an extra (empty) field most of the time, and there's no way to know when. For example, 'a@b' incorrectly returns 3 fields, although 'a@' correctly returns 2.
      That's weird. I'm trying to figure out why mine takes another pass after encountering end of string (even if I specify that the separator can be end-of-string). Meanwhile, I'll point out that your method finds only one field in '@' (instead of two empty ones — if there's no newline after the @).

      Update: this seems to work:

      my @pieces = /(?:^|\G@)((?:#.|[^@])*)/sg;
      Still don't know why I couldn't get it to realize it had hit end of string. My guess is, if end-of-string is not required to match, it takes another shot to make sure nothing matches.

      Caution: Contents may have been coded under pressure.

        Still don't know why I couldn't get it to realize it had hit end of string

        It's seen as somewhat of a bug. It happens when the search expression can match a zero length string. In the followig, the parens indicate what the three passes of your earlier s/// matches:

        (a@)(b)()

        Or in terms of @- and @+:
        pass@-@+
        102
        223
        333

        The /g loop ends when the fourth pass matches the same thing as the third pass and pos+1 is beyond the end of the string.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (8)
As of 2024-04-23 08:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found