Menu

#50 regsplit primitive

None
closed-accepted
5
2016-08-27
2003-10-30
No

There should be a "regsplit" or "regexplode" primitive
that splits a string on matches of a regular
expression, like Perl's "split" function. e.g.:

"potassium" "[aeiou]+" regsplit

would return:

{"p", "t", "ss", "m"}

Discussion

  • Revar Desmera

    Revar Desmera - 2003-11-03
    Logged In: YES
    user_id=6331

    regsplit[ str:str str:pat -- list:matches ]
    "\r#&$%^\r" var! marker
    str @ pat @ marker @ REG_ALL
    marker @ explode_array
    ;

    ( or, arguably more pedanticly correct: )

    regsplit[ str:str str:pat -- list:matches ]
    pat @ "^\(.*\)%s\(.*\)$" fmtstring pat !
    { }list var! out
    begin
    str @ pat @ 0 regexp if
    dup 1 [] out @ []<- out !
    2 [] dup strlen str @ strlen = if
    1 strcut swap pop
    then
    str !
    else
    str @ out @ []<- exit
    then
    repeat
    ;

     
  • Revar Desmera

    Revar Desmera - 2003-11-03

    Logged In: YES
    user_id=6331

    Is possible the marker string in my previous comment should be
    different, so as not to have any &s or stuff that regsub may
    expand. I just wrote that off the top of my head. Idea is to use a
    merker string that is highly unlikely to occur in the string you
    expect to split.

     
  • Kyle Hamilton

    Kyle Hamilton - 2005-07-06

    Logged In: YES
    user_id=7718

    Still, it'd be quicker to implement it in the server.

    Natty, do you consider this one done enough? Or should it
    stay open and in the feature requests?

     
  • Kyle Hamilton

    Kyle Hamilton - 2005-07-06
    • assigned_to: nobody --> winged
     
  • Wyld

    Wyld - 2016-08-27
    • status: open --> closed-accepted
    • Group: -->
     
  • Wyld

    Wyld - 2016-08-27

    Added REGSPLIT and REGSPLIT_NOEMPTY in FB7.

     

Log in to post a comment.

MongoDB Logo MongoDB